I’ve encountered a problem with generating reverse url in templates in django. I’m trying to solve it since a few hours and I have no idea what the problem might be. URL reversing works great in models and views:
# like this in models.py
@models.permalink
def get_absolute_url(self):
return ('entry', (), {
'entry_id': self.entry.id,
})
# or this in views.py
return HttpResponseRedirect(reverse('entry',args=(entry_id,)))
but when I’m trying to make it in template I get such an error:
NoReverseMatch at /entry/1/
Reverse for ”add_comment” with arguments ‘(1L,)’ and keyword arguments ‘{}’ not found.
My file structure looks like this:
project/
├── frontend
│ ├── models.py
│ ├── urls.py
│ └── views.py
├── settings.py
├── templates
│ ├── add_comment.html
│ └── entry.html
├── utils
│ └── with_template.py
└── wsgi.py
My urls.py:
from project.frontend.views import *
from django.conf.urls import patterns, include, url
urlpatterns = patterns('project.frontend.views',
url(r'^entry/(?P<entry_id>\d+)/', 'entry', name="entry"),
(r'^entry_list/', 'entry_list'),
Then entry_list.html:
{% extends "base.html" %}
{% block content %}
{% for entry in entries %}
{% url 'entry' entry.id %}
{% endfor %}
{% endblock %}
In views.py I have:
@with_template
def entry(request, entry_id):
entry = Entry.objects.get(id=entry_id)
entry.comments = entry.get_comments()
return locals()
where with_template is following decorator(but I don’t think this is a case):
class TheWrapper(object):
def __init__(self, default_template_name):
self.default_template_name = default_template_name
def __call__(self, func):
def decorated_func(request, *args, **kwargs):
extra_context = kwargs.pop('extra_context', {})
dictionary = {}
ret = func(request, *args, **kwargs)
if isinstance(ret, HttpResponse):
return ret
dictionary.update(ret)
dictionary.update(extra_context)
return render_to_response(dictionary.get('template_name',
self.default_template_name),
context_instance=RequestContext(request),
dictionary=dictionary)
update_wrapper(decorated_func, func)
return decorated_func
if not callable(arg):
return TheWrapper(arg)
else:
default_template_name = ''.join([ arg.__name__, '.html'])
return TheWrapper(default_template_name)(arg)
Do you have any idea, what may cause the problem?
Great thanks in advance!
EDIT:
I’ve just made an experiment. I commented everything I could leaving only:
urls.py
# -*- coding: UTF-8 -*-
from justmetyou.frontend.views import *
from django.conf.urls import patterns, include, url
urlpatterns = patterns('justmetyou.frontend.views',
url(r'^entry/(?P<entry_id>\d+)/', 'entry', name="entry"),
)
views.py
# -*- coding: UTF-8 -*-
from justmetyou.frontend.models import *
from django.shortcuts import render_to_response, get_object_or_404
def entry(request, entry_id):
entry = Entry.objects.get(id=entry_id)
return render_to_response('entry.html', {'entry':entry})
entry.html
{% extends "base.html" %}
{% block content %}
{% url 'entry' 1 %}
{% endblock %}
And nothing changed. I’m getting the same error all the time.
So now I’m much more confused, because in this simple code everything looks fine. Do you think it is possible that it’s a matter of external stuff like server or smth like this…? I don’t have any idea…
I suspect you are using Django 1.3 and you didn’t call
{% load url from future %}.So try instead without
'around the name of the URL: