0

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>
0

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

I still get an error. NoReverseMatch at /author/list/4 Reverse for 'author_update' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['author/(?P<user_id>\\d+)/(?P<pk>\\d+)$']
I believe that that problem is unrelated to the problem you've asked in this post. To diagnose the problem more information about it is needed.. such as the code from the views and urls as well as the error stack trace
I already check your answer if you can help in my other question, thanks.
I've answered it -- let me know if it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.