Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django url tag multiple parameters

I have two similar codes. The first one works as expected.

urlpatterns = patterns('',
                       (r'^(?P<n1>\d)/test/', test),
                       (r'', test2),
{% url testapp.views.test n1=5 %}

But adding the second parameter makes the result return empty string.

urlpatterns = patterns('',
                           (r'^(?P<n1>\d)/test(?P<n2>\d)/', test),
                           (r'', test2),)


{% url testapp.views.test n1=5, n2=2 %}

Views signature:

def test(request, n1, n2=1):
like image 365
Overdose Avatar asked Apr 04 '10 20:04

Overdose


People also ask

How do I pass multiple parameters in URL?

To add a parameter to the URL, add a /#/? to the end, followed by the parameter name, an equal sign (=), and the value of the parameter. You can add multiple parameters by including an ampersand (&) between each one.

What is namespace Django?

URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It's a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed.


2 Answers

Try

{% url testapp.views.test n1=5,n2=2 %}

without the space between the arguments

Update: As of Django 1.9 (and maybe earlier) the correct way is to omit the comma and separate arguments using spaces:

{% url testapp.views.test n1=5 n2=2 %}
like image 138
naw Avatar answered Sep 21 '22 09:09

naw


Here's an actual example of me using this technique. Maybe this will help:

{% if stories %}
  <h2>Stories by @{{author.username}}</h2>
  <ul>
    {% for story in stories %}
      <li><a href="{% url 'reader:story' author.username story.slug %}">{{story.title}}</a></li>
    {% endfor %}
  </ul>
{% else %}
  <p>@{{author.username}} hasn't published any stories yet.</p>
{% endif %}
like image 37
Jesse Lawson Avatar answered Sep 23 '22 09:09

Jesse Lawson