8

I am trying to design URLconf file, where one of the views accepts two optional parameters: date and 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??
)

user may visit url which contains one OR both parameters.

Without arguments:

http://example.com/dashboard

With date (ddmmyyy format) only should look like:

http://example.com/dashboard/01042015

With account UUID only:

http://example.com/dashboard/e1c0b81e-2332-4e5d-bc0a-895bd0dbd658

Both date and account uuid:

http://example.com/dashboard/01042015/e1c0b81e-2332-4e5d-bc0a-895bd0dbd658

How should I design my URLconf? It should be easly readable and fast.

Thanks!

1 Answer 1

14

Using multiple patterns is probably the easiest way to 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'),
)
Sign up to request clarification or add additional context in comments.

1 Comment

The question is,is this correct? by correct meaning a good practice?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.