Hi,
AFAIK, the recommended way in Django to handle multiple urls with the same view, is to have optional parameters. (
https://docs.djangoproject.com/en/1.8/topics/http/urls/#specifying-defaults-for-view-arguments)
For example,
in url.py:
url(r'^blog/page/(?P<page_pk>\d+)/$', views.page, name='view-page'),
url(r'^blog/page/(?P<page_pk>\d+)/(?P<page_slug>[a-zA-Z0-9_.-])/$', views.page, name='view-page'),
in views.py:
# View (in blog/views.py)
def page(request, page_pk, page_slug=""):
However, in the templates, the url template tag can't accept optional parameters.
So instead of writing {% url 'view-page' page_pk page_slug %}, you need to write {% if page_slug=="" %}{% url 'view-page' page_pk %}{% else %}{% url 'view-page' page_pk page_slug %}{% endif %}
On the project I'm working on I have 2 or more optional parameters, and it's a real nuisance.
The url tag could have done this logic by itself. If any of its inputs are blank or none, disregard it.
Same question from Stack Overflow (not asked by me) :
http://stackoverflow.com/questions/16870884/url-template-optional-paramThanks