python - Same view with multiple URL patterns and optional arguments -
i trying design urlconf file, 1 of views accepts 2 optional parameters: date
, account_uuid
.
views.py
:
@login_required def dashboard(request, date=none, account_uuid=none): # unrelated code...
urls.py
:
urlpatterns = patterns( 'app.views', url(r'^dashboard$', 'dashboard', name='dashboard'), #what here?? )
user may visit url contains 1 or both parameters.
without arguments:
with date (ddmmyyy format) should like:
with account uuid only:
http://example.com/dashboard/e1c0b81e-2332-4e5d-bc0a-895bd0dbd658
both date , account uuid:
http://example.com/dashboard/01042015/e1c0b81e-2332-4e5d-bc0a-895bd0dbd658
how should design urlconf? should easly readable , fast.
thanks!
using multiple patterns easiest way achieve this:
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'), )
Comments
Post a Comment