Reputation: 20875
I am trying to retrieve the URL of a view in a template via
{% url 'session_views.login_user' %}
My urls.py looks like
from django.conf.urls.defaults import *
urlpatterns = patterns('myappname',
# Greets user on the home page.
(r'^$', 'session_views.index'),
# Session
(r'^login/?$', 'session_views.login_user'),
)
However, I get the following error.
TemplateSyntaxError at /
Caught NoReverseMatch while rendering:
Reverse for ''session_views.login_user'' with arguments '()'
and keyword arguments '{}' not found.
Why am I erring? I clearly defined a view called login_user
in session_views.py.
Upvotes: 0
Views: 76
Reputation: 6320
Look at the error Reverse for ''session_views.login_user''
, it has two sets of single quotes. You do not use the quotes in the template - it should read:
{% url session_views.login_user %}
Or you can name your views - after which you would be able to call them via the name, and it would need the quotes:
urls.py:
urlpatterns = patterns('myappname',
# Greets user on the home page. (r'^$', 'session_views.index', name="index_view"),
# Session (r'^login/?$', 'session_views.login_user', name="login_user"), )
template url reverse:
{% url "login_user" %}
EDIT:
After seeing Tomita's
answer, I may have misunderstood the question. I thought you were trying to retrieve the URL as in follow it, and you may be trying to retrieve the url as a variable. My answer applies to following the URL, as in <a href="{% url session_views.login_user %}"....
. Just in case, I will leave it.
Upvotes: 2
Reputation: 118448
You're probably looking at the dev branch documentation, which changes the URL tag functionality.
For django <1.5, you should either follow the appropriate docs, or future proof your code by loading the future url tag.
{% load url from future %}
In Django 1.5, the behavior of the url template tag will change, with the first argument being made into a context variable, rather than being a special case unquoted constant. This will allow the url tag to use a context variable as the value of the URL name to be reversed.
In order to provide a forwards compatibility path, Django 1.3 provides a future compatibility library -- future -- that implements the new behavior. To use this library, add a load call at the top of any template using the url tag, and wrap the first argument to the url tag in quotes. For example:
{% load url from future %} {% url 'myapp:view-name' %} The new library also drops support for the comma syntax for separating arguments to the url template tag.
In Django 1.5, the old behavior will be replaced with the behavior provided by the future tag library. Existing templates be migrated to use the new syntax.
Upvotes: 2