Django, url tag in template doesn't work: NoReverseMatch
Posted
by
Lukasz Jocz
on Stack Overflow
See other posts from Stack Overflow
or by Lukasz Jocz
Published on 2012-10-19T22:59:42Z
Indexed on
2012/10/19
23:00 UTC
Read the original article
Hit count: 222
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!
© Stack Overflow or respective owner