uses uses - 1 year ago 84
Python Question

How to use underscore to separate named group in Django URL?

I am new to django field and would like to seek help from django masters.

Now my URL has two named groups (product_slug and platform_slug) separated by "/" as below:


Current URL Config:

url(r'^search/(?P<product_slug>[\w\-]+)$', CustomSearchView(), name='search_result_detail'),
url(r'^search/(?P<product_slug>[\w\-]+)/(?P<platform_slug>[\w\-]+)$', CustomSearchView(), name='search_result_platform'),

I would like to use underscore as separator to make the URL short and clean:


Seems it's not easy to use separator other than "/" to separate Named Groups.
Are there any other solutions?

Answer Source

It's a regular expression, you can use any characters you like. The only thing is that you need to be more restrictive in your capturing groups, since \w also matches underscores.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download