How can I enable project id parameter of DJango to be optional

0 votes

I have a Django URL like this:

url(
    r'^project_config/(?P<product>\w+)/(?P<project_id>\w+)/$',
    'tool.views.ProjectConfig',
    name='project_config'
),

views.py:

def ProjectConfig(request, product, project_id=None, template_name='project.html'):
    ...
    # do stuff

The problem is that I want the project_id parameter to be optional.

I want /project_config/ and /project_config/12345abdce/ to be equally valid URL patterns, so that if project_id is passed, then I can use it.

As it stands at the moment, I get a 404 when I access the URL without the project_id paramete

Jul 3, 2020 in Python by kartik
• 37,520 points
912 views

1 answer to this question.

0 votes

Hello @kartik,

There are several approaches.

One is to use a non-capturing group in the regex: (?:/(?P<title>[a-zA-Z]+)/)?
Making a Regex Django URL Token Optional

Another, easier to follow way is to have multiple rules that matches your needs, all pointing to the same view.

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),
)

Keep in mind that in your view you'll also need to set a default for the optional URL parameter, or you'll get an error:

def foo(request, optional_parameter=''):
    # Your code goes here

Hope it works!!

Thank you!!

answered Jul 3, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How can I use Django database layer outside of Django?

Hello @kartik, You just need to configure the ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,840 points
2,410 views
0 votes
1 answer

How can I enable CORS on Django REST Framework?

Hello @kartik, Take the following steps to enable ...READ MORE

answered Jun 30, 2020 in Python by Niroj
• 82,840 points
4,240 views
0 votes
1 answer

How can I enable CORS on Django REST Framework

Hello @kartik, You can do by using a ...READ MORE

answered Jul 1, 2020 in Python by Niroj
• 82,840 points
2,005 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,437 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,555 views
0 votes
1 answer

How can I get the domain name of my site within a Django template?

Hello kartik, The variation of the context processor ...READ MORE

answered Apr 23, 2020 in Python by Niroj
• 82,840 points
9,804 views
0 votes
1 answer

How do I add a link from the Django admin page of one object to the admin page of a related object?

Hello @kartik, Set show_change_link to True (False by default) in your inline ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,840 points
10,589 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP