Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道是否有必要在我的 urls.py 中有这两种模式:
url(r'^books/author/(?P<id>\d+)/$', 'books.views.author'), url(r'^books/author/(?P<id>\d+)/(?P<slug>[-\w]+)/$', 'books.views.author'),
基本上,slug 是可选的。而视图函数定义是这样的:
def author(request, id, slug=None):
请指教。
您可以将第二组和斜线包装在一个非捕获组中,并使整个组可选:
r'^books/author/(?P<id>\d+)/(?:(?P<slug>[-\w]+)/)?$'