Categories
Python Answers

How to add optional URL parameters with Python Django?

Spread the love

To add optional URL parameters with Python Django, we call add multiple rules for the same URL.

For instance, we write

urlpatterns = patterns('',
    url(r'^project_config/$', views.foo),
    url(r'^project_config/(?P<product>\w+)/$', views.foo),
    url(r'^project_config/(?P<product>\w+)/(?P<project_id>\w+)/$', views.foo),
)

to add 3 URL patterns that has no URL parameters, the product parameter only, and the product and project_id` parameters.

And we map all 3 to the same view.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *