0

I'm new to Django and puzzled. Using Django 1.4. Inside one of my templates, this code works:

{% for element0, element1 in menu.elements %}
    <li class='menu_{{ name }}'><a href="{% url users.views.home %}">{{ element0 }}</a></li>
{% endfor %}

... but this code throws a "NoReverseMatch" error:

{% for element0, element1 in menu.elements %}
    <li class='menu_{{ name }}'><a href="{% url element1 %}">{{ element0 }}</a></li>
{% endfor %}

... despite the fact that the "element1" variable holds 'users.views.home'. I'm thinking/hoping that the solution to this is really simple... that I've missed something obvious about variable handling inside Django templates?

I've consulted the documentation for the url built-in function to no avail. Any help will be appreciated.

codemonkey
  • 2,661
  • 2
  • 24
  • 34

2 Answers2

1

I think you need to add this to your template:

{% load url from future %}

and change the first call to

 {% url 'users.views.home' %}

see the forwards compatibility note in the docs you linked to

scytale
  • 12,346
  • 3
  • 32
  • 46
1

That's bad idea to write like this {% url 'users.views.home' %}, better use named url - {% url 'users_home' %}, it will be easy to maintain in the future. For example if you decide to move your def home(request) from users.views to account.views you will need to replace all urls occurrences in all your templates. But if you use named urls, you just need to change urls.py

Alexander Larikov
  • 2,328
  • 15
  • 15