使用基于django类的视图键入错误 [英] Type error with django class based view
本文介绍了使用基于django类的视图键入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到这个错误
/ author / list / 4中的TypeError
super(type,obj):obj必须是类型的实例或子类型
TypeError at /author/list/4 super(type, obj): obj must be an instance or subtype of type
异常位置:/home/ronald/best/A2/0124/vort/larb/views.py in get_context_data,第140行
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' ),
view.py for listview
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
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>
推荐答案
代码应该是:
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
In context = Super(...).get_context_data, I changed it from AuthorCreate to AuthorList
这篇关于使用基于django类的视图键入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文