使用基于django类的视图键入错误

时间:2014-01-29 19:37:23

标签: django django-views django-class-based-views

我收到此错误

/ author / list / 4中的TypeError super(type,obj):obj必须是

类型的实例或子类型

异常位置:get_context_data,第140行中的/home/ronald/best/A2/0124/vort/larb/views.py

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

url.py

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

viewsview用于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 个答案:

答案 0 :(得分:4)

代码应为:

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

在context = Super(...)。get_context_data中,我将它从AuthorCreate更改为AuthorList