D
D
DUDE2020-05-01 03:57:06
Django
DUDE, 2020-05-01 03:57:06

Why does display on generic classes (Generic Editing Views) work incorrectly?

Hello everyone, I'm sitting here looking at the code and I don't understand what's wrong.

I have urls.py

urlpatterns = [
    ...
    path(".../author/<id>/update/", AuthorUpdateView.as_view(), name="author-update-url"),
    ...


Here is views.py
...

class AuthorUpdatePageView(UpdateView):
    model = Author
    fields = ["first_name", "last_name", "date_of_birth", "date_of_death"]

    # В гайде работает без этого метода, пришлось переопределить
    def get_object(self, queryset=None):
        return Author.objects.get(id=self.kwargs.get("id"))
...

In general, in the guide, everything works without rewriting the get_object () method. Moreover, what is strange, with this method and without this method, the final paths are the same. Without the method ".../author/1/update/" and with this method ".../author/1/update/".

Gives an error message:
Generic detail view AuthorUpdatePageView must be called with either an object pk or a slug in the URLconf.


In general, it is not difficult to rewrite the method, but nevertheless, for what reason does it not work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-05-01
@stympel

Well, they obviously write to you that they are waiting for pk or slug, and you put the id in urls.py, although in most cases this is the same, but the name is different.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question