Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse/name urls with TemplateView.as_view?

Tags:

django

I'm trying to use TemplateView.as_view() in urls.py then name it using ye olde templatetag url. Should this work? Or am I just doing it wrong..? Or is it some of the legacy crap in my app interfering? I've got...

<li><a href="{% url "legal" %}">Terms and Conditions</a></li>

and

url(r'^legal/$', TemplateView.as_view(template_name="legal.html"), name="legal"),

Which yields

NoReverseMatch at /how-it-works/
Reverse for '"legal"' with arguments '()' and keyword arguments '{}' not found.

Seems to me this ain't ever gonna work this way.

How do we reverse urls with TemplateView?

like image 415
John Mee Avatar asked Oct 22 '22 05:10

John Mee


1 Answers

The code you have posted is syntactically correct, but there is something else interfering.

  • That url tag is Django 1.5+ syntax (note the warning in the docs here), can you confirm that is the install you are using? I suspect this is the the problem, the double quoting of legal in the exception message is very suspicious. A Django 1.5+ exception message would contain 'legal', not '"legal"' for a URL that is not found by the dispatcher.
  • Is the url being registered by the URL dispatcher? You can tell in debug mode by hitting a bad URL (ie 404), the output will list all valid url paths from your URLConf.

Add more info if you continue to have problems so we can isolate further.

Good luck!

like image 138
markdsievers Avatar answered Nov 15 '22 10:11

markdsievers