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-param
Thanks
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/4b411472-df53-481b-b1c2-f1f8383dd7e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.