我正在尝试设计URLconf文件,其中一个视图接受两个可选参数:date
和account_uuid
。
views.py
:
@login_required
def dashboard(request, date=None, account_uuid=None):
# some unrelated code...
urls.py
:
urlpatterns = patterns(
'app.views',
url(r'^dashboard$',
'dashboard',
name='dashboard'),
#WHAT HERE??
)
用户可以访问包含一个OR参数的url。
没有参数:
日期(ddmmyyy格式)只应如下所示:
仅限帐户UUID:
http://example.com/dashboard/e1c0b81e-2332-4e5d-bc0a-895bd0dbd658
日期和帐户均为uuid:
http://example.com/dashboard/01042015/e1c0b81e-2332-4e5d-bc0a-895bd0dbd658
我应该如何设计我的URLconf?它应该易于阅读和快速。
谢谢!
答案 0 :(得分:7)
使用多种模式可能是实现这一目标的最简单方法:
urlpatterns = patterns('app.views',
url(r'^dashboard$', 'dashboard', name='dashboard'),
url(r'^dashboard/(?P<date>[\d]{8})/$', 'dashboard', name='dashboard'),
url(r'^dashboard/(?P<account_uid>[\da-zA-Z-]{36})/$', 'dashboard', name='dashboard'),
url(r'^dashboard/(?P<date>[\d]{8})/(?P<account_uid>[\da-z-]{36})/$', 'dashboard', name='dashboard'),
)