我收到此错误
/ 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>
答案 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