21

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

3 Answers 3

35

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 %}
Sign up to request clarification or add additional context in comments.

1 Comment

Is there any documentations available for this feature in Django Docs?
4

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 %}

Comments

2

https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#url

From Django 1.5 Warning Don’t forget to put quotes around the function path or pattern name!

{% url 'some-url-name' arg1=v1 arg2=v2 %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.