Question

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>
Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top