问题
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