Type error with django class based view

旧城冷巷雨未停 提交于 2019-12-12 02:37:43

问题


I get this error

TypeError at /author/list/4 super(type, obj): obj must be an instance or subtype of type

Exception Location: /home/ronald/best/A2/0124/vort/larb/views.py in get_context_data, line 140

context = super(AuthorCreate, self).get_context_data(**kwargs)

url.py

url(r'^author/list/(?P<user_id>\d+)$', AuthorList.as_view(), name='author_list' ),

views.py for listview

class AuthorList(LoginRequiredMixin, ListView):
    template_name = 'authorList.html'
    queryset = Author.objects.all()

    def get_context_data(self, **kwargs):
        context = super(AuthorCreate, self).get_context_data(**kwargs)
        if int(self.kwargs['user_id']) != self.request.user.id:
            raise PermissionDenied
        return context

authorList.html

   {{ request.user.username}} 

    <ul>
        {% for author in object_list %}
            <li>{{ author.firstName }}
                 <a href="{% url "author_update" author.id %}">{{ author.firstName }}</a>
                 <a href="{% url "author_delete" author.id %}">delete</a>
            </li>
         {% endfor %}
    </ul>

回答1:


The code should be this:

class AuthorList(LoginRequiredMixin, ListView):
    template_name = 'authorList.html'
    queryset = Author.objects.all()

    def get_context_data(self, **kwargs):
        context = super(AuthorList, self).get_context_data(**kwargs)
        if int(self.kwargs['user_id']) != self.request.user.id:
            raise PermissionDenied
        return context

In context = Super(...).get_context_data, I changed it from AuthorCreate to AuthorList



来源:https://stackoverflow.com/questions/21440891/type-error-with-django-class-based-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!