8

I don't know if this SO question is of the same problem that I am about to describe, but it does share the same symptoms. Unfortunately, it still remains unresolved as I am writing.

So here is my problem. I am trying to add James Bennett's django-registration app to my django project. I have pretty much finished configuring it to my needs - custom templates and urls. Just when I thought everything was good to go. I got NoReverseMatch error from using {% url 'testing' item_id=123 %} (I also tried using the view name, myapp.views.test, instead but no luck) in one of the custom templates required by django-registration. Interestingly, I tried reverse('testing', kwargs={'item_id':123}) in the shell and the url was returned just fine. I thought {% url %} uses reverse() in the back-end but why did I get different outcomes?

urls.py: (the URLconf of my site)

urlpatterns = patterns('myapp.views',
    url(r'^test/(?P<item_id>\d+)/$', 'test', name='testing'),
)

activation_email.txt: (the said template. Note it's intentionally in .txt extension as required by django-registration and that shouldn't be the cause of the problem.)

{% comment %}Used to generate the body of the activation email.{% endcomment %}
Welcome to {{ site }}! Please activate your account by clicking on the following link:

{% url 'testing' item_id=123 %}

Note the activation link/code will be expired in {{ expiration_days }} days.

I don't know if it matters but just thought I should mention activation_email.txt is stored in the templates directory of myapp though it is used by django-registration.

Also, I am using django 1.4

I have a feeling that the problem has something to do with the url namespaces, a topic that I have never understood, but it's just a naive guess. (IMO, the django documentation is great in explaining everything about django, except when it comes to url namespaces)

Community
  • 1
  • 1
tamakisquare
  • 16,659
  • 26
  • 88
  • 129

1 Answers1

12

I'm no expert here, but in a Django project I'm working on at the moment I use the name of the url without quotes. I just added quotes around a similar line in one of my templates and it produced the same error as your error.

Try:

{% url testing item_id=123 %}
John
  • 529
  • 3
  • 7
  • 1
    Interesting~ that works!! Thanks a lot. But what's the rationale behind this? I mean all of the examples of `{% url %}` in the django docs use quotes. – tamakisquare Apr 27 '12 at 23:41
  • Check the named urls section of the docs: https://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns As far as I can tell, if you are using url tags with an actual path to the view then you need the quotes. If using a name, you don't. – John Apr 27 '12 at 23:46
  • 2
    Thanks for the reference. In the template tags document, there is an example `{% url 'myapp:view-name' %}`, so I thought qoutes were needed for using view name. I guess that applies only when I use view name together with namespace, which is a bit confusing. – tamakisquare Apr 27 '12 at 23:51
  • 7
    @ahmoo, they are getting rid of the unquoted url tag in 1.5, you can `{% load url from future %}` to use the new tag behavior which always uses quotes, unless of course you are passing in a variable which resolves to a string. – Yuji 'Tomita' Tomita Apr 27 '12 at 23:54
  • After a little experimentation, what I said earlier about needing quotes if you use the path.to.the.view is not true. I added quotes to other url tags and got the same error. Seems that no quotes at all is the way to go. I'm using 1.4. Interesting bit of info from @YujiTomita regarding 1.5 – John Apr 28 '12 at 00:05
  • 3
    @John - Here is the [link](https://docs.djangoproject.com/en/dev/releases/1.3/#changes-to-url-and-ssi) to the info mentioned by YujiTomita. It should have been also put into the relevant sections of the official docs. – tamakisquare Apr 28 '12 at 00:10