Django - url with no arguments to get the default value

Assume that it urltakes an argument (here book_id) and passes the value to views:

url(r'^/(?P<book_id>\w+)/$', 'pro.views.book', name='book'),

Is it possible that url accepts an argument, but if no argument is given, take the default value. If possible, perhaps in viewstoo. I apologize if this is a lame question, but I really need to know. Any help or suggestion would be greatly appreciated. thank you

+4
source share
1 answer

. :

url(r'^(?:(?P<book_id>\w+)/)?$', 'pro.views.book', name='book'),

:

def book(request, book_id='default'):

, URL , :

url(r'^(?P<book_id>\w+)/$', 'pro.views.book', name='book'),
url(r'^$', 'pro.views.book', name='default_book'),

, . , .

+11

All Articles