Erik
Erik

Reputation: 7731

Django {% url %} reverse not working

I have a view in a Django 1.4 project:

def index(request):
    print reverse('menus_index')
    latest_menu_list = Menu.objects.all().order_by('name')
    return render_to_response('menus/index.html', {'latest_menu_list': latest_menu_list})

This works as expected and prints out the reversed URL which is /menus/.

Inside of the index.html template (which is called by this view) I have:

{% url menus_index %}

Which causes a NoReverseMatch at /menus/ error. Reverse for '' with arguments '()' and keyword arguments '{}' not found.

My application's urls.py is:

urlpatterns = patterns('menus.views',
url(r'^$','index', name='menus_index'),
url(r'^(?P<menu_id>\d+)/$','detail', name='menus_detail'),
)

Which is included in my project's urls.py file.

What am I doing wrong?

Update:

Here is the full index.html template code:

{% extends "base.html" %}
{% load url from future %}

{% block title %}
Menu Index
{% endblock %}

{% block content %}
{% if latest_menu_list %}
<ul>
    {% for menu in latest_menu_list %}
    <li><a href="{% url menus_index %}/{{ menu.id }}/">{{ menu.name }}</a></li>
    {% endfor %}
</ul>
{% else %}
   <p>No menus are available.</p>
{% endif %}
{% endblock %}

Upvotes: 0

Views: 5948

Answers (3)

yanik
yanik

Reputation: 808

You should use add variable for reverse, somethink like this: {% url "menus_index" menu.slug %}

Upvotes: 0

Erik
Erik

Reputation: 7731

Answer: use {% url 'menus_index' %}. That {% load url from future %} makes the quotes a requirement per https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#url

Upvotes: 6

ssbb
ssbb

Reputation: 2025

Maybe this not menus_index? Paste full template code this.

Upvotes: 0

Related Questions