首页 > 解决方案 > “WSGIRequest”对象没有属性“模板”(在 django 中呈现基于类的视图时出现属性错误)

问题描述

我的视图所做的是从 id 获取偶数,并返回一个包含所有详细信息的表格的 html 页面。

这是一个足够简单的视图:

class Edetails(View, SuperuserRequiredMixin):
    template = 'events/details.html'

    def get(request, self, pk):
        event = Event.objects.get(id=pk)
        count = event.participants.count()
        participants = event.participants.all()
        ctx = {
            'count': count,
            'participants': participants,
            'pk': pk
        }
        return render(request, self.template, ctx)

这是 SuperUserRequiredMixin:

class SuperuserRequiredMixin(AccessMixin):
    """
    Mixin allows you to require a user with `is_superuser` set to True.
    """
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_superuser:
            return self.handle_no_permission(request)

        return super(SuperuserRequiredMixin, self).dispatch(
            request, *args, **kwargs)

和 Access Mixin:

class AccessMixin(object):
    """
    'Abstract' mixin that gives access mixins the same customizable
    functionality.
    """
    login_url = None
    raise_exception = False
    redirect_field_name = REDIRECT_FIELD_NAME  # Set by django.contrib.auth
    redirect_unauthenticated_users = False

    def get_login_url(self):
        """
        Override this method to customize the login_url.
        """
        login_url = self.login_url or settings.LOGIN_URL
        if not login_url:
            raise ImproperlyConfigured(
                'Define {0}.login_url or settings.LOGIN_URL or override '
                '{0}.get_login_url().'.format(self.__class__.__name__))

        return force_string(login_url)

    def get_redirect_field_name(self):
        """
        Override this method to customize the redirect_field_name.
        """
        if self.redirect_field_name is None:
            raise ImproperlyConfigured(
                '{0} is missing the '
                'redirect_field_name. Define {0}.redirect_field_name or '
                'override {0}.get_redirect_field_name().'.format(
                    self.__class__.__name__))
        return self.redirect_field_name

    def handle_no_permission(self, request):
        if self.raise_exception:
            if (self.redirect_unauthenticated_users
                    and not request.user.is_authenticated):
                return self.no_permissions_fail(request)
            else:
                if (inspect.isclass(self.raise_exception)
                        and issubclass(self.raise_exception, Exception)):
                    raise self.raise_exception
                if callable(self.raise_exception):
                    ret = self.raise_exception(request)
                    if isinstance(ret, (HttpResponse, StreamingHttpResponse)):
                        return ret
                raise PermissionDenied

        return self.no_permissions_fail(request)

    def no_permissions_fail(self, request=None):
        """
        Called when the user has no permissions and no exception was raised.
        This should only return a valid HTTP response.
        By default we redirect to login.
        """
        return redirect_to_login(request.get_full_path(),
                                 self.get_login_url(),
                                 self.get_redirect_field_name())

但是,当我单击链接向视图发送获取请求时:

<a href="{% url 'events:Edetails' event.id %}">View Participants</a>

我收到此错误消息:

/Edetails2 处的 AttributeError

“WSGIRequest”对象没有属性“模板”

我不明白这是什么意思。我怎样才能解决这个问题?

标签: djangodjango-viewsdjango-templatesdjango-wsgi

解决方案


推荐阅读