P
P
Peterson_s2020-10-16 17:51:10
Django
Peterson_s, 2020-10-16 17:51:10

How to pass Url template to DetailView?

Hello. I just started learning Django and I have a question.
Here is urls.py

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^books/$', views.BookListView.as_view(), name='books'),
    url(r'^book/(?P<pk>\d+)$', views.BookDetailView.as_view(), ),
    url(r'^authors/$', views.AuthorListView.as_view(), name='authors'),
    url(r'^author/(?P<pk>\d+)$', views.AuthorDetailView.as_view(), name='author-detail')
]


here is views.py
class BookListView(generic.ListView):
    model = Book
    paginate_by = 10

   

class BookDetailView(generic.DetailView):
    model = Book
    

class AuthorListView(generic.ListView):
    model = Author
    


class AuthorDetailView(generic.DetailView):
    model = Author

how do i pass pk to views.py using DetailView&

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-10-16
@Peterson_s

how do i pass pk to views.py using DetailView&

It catches pk / id itself, it is not necessary to transfer, the main thing is to specify this variable in the address.
django3+

У вас кажется древняя документация по джанге, сейчас так урлы не строят.
Правильнее было бы так:
urlpatterns = [
    path('', views.index, name='index'),
    path('books/', views.BookListView.as_view(), name='books'),
    path('book/<int:pk>/', views.BookDetailView.as_view(), ),
    path('authors/', views.AuthorListView.as_view(), name='authors'),
    path('author/<int:pk>/', views.AuthorDetailView.as_view(), name='author-detail')
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question