1

I have a django app set up consisting of ListViews, TemplateViews etc.. So, I just added a small templateview to it like so:

#views.py
class TermsTemplateView(TemplateView):
    template_name = "terms.html"

#urls.py
url(r'^terms/$', TermsTemplateView.as_view(), name='terms'),

and in terms.html, I am using for linking:

<a href="{% url 'terms' %}">Terms & Conditions</a>

For some strange reason, I keep getting 404 on localhost/terms as follows:

404: No <model_name> found matching the query

I am baffled why this is happening all of a sudden. I have the same set up for "about", "thanks", "contact" pages, and they seem to display it with no problems.

..and the worst part is, if I modify the urls.py like so:

url(r'^/terms/$', TermsTemplateView.as_view(), name='terms'),

and then go to http://127.0.0.1:8000//terms/ - the page seems to be there.. I am surprised why this is so :(

Any help would enlighten me!

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
JohnJ
  • 6,736
  • 13
  • 49
  • 82
  • What about the `/` at the end of your regex - what if you remove it and try `localhost/terms`? If it doesn't help, please show your `urls.py`. – alecxe Aug 02 '13 at 18:59
  • Jeez! you are correct. When I take the `/` out of the urls.py and go `localhost/terms` it works. Why so?? I am confused now. Also, please make it as an answer - I will accept it. – JohnJ Aug 02 '13 at 19:02

1 Answers1

1

The / at the end is the culprit of your problems. localhost/terms doesn't match '^terms/$' regular expression, localhost/terms/ does.

You can make / at the end optional by using ?:

url(r'^terms/?$', TermsTemplateView.as_view(), name='terms'),

UPD: Note that there is a better solution to the problem, APPEND_SLASH:

When set to True, if the request URL does not match any of the patterns in the URLconf and it doesn’t end in a slash, an HTTP redirect is issued to the same URL with a slash appended.

Also see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Just wondering: how come with other urls seem to work with thw `/` then? What is the best practice around this? Thanks for all your help. – JohnJ Aug 02 '13 at 19:17
  • Sure, please see `UPD` section of the answer. – alecxe Aug 02 '13 at 19:25
  • THanks.. following up...but the default of APPEND_SLASH is true - so, do I still need to put this in my settings.py? – JohnJ Aug 02 '13 at 19:30
  • Yeah, you are right. It's not actually about using `APPEND_SLASH`, it's more about understanding how does it work if set to `True`. First, it tries to match the url to regexp without the slash, then, if no match, it goes through your `urlpatterns` one more time, with appended slash. And, most likely, it matches wrong item, something before `^terms/$`..can you show your `urls.py`? – alecxe Aug 02 '13 at 19:35
  • OK. I see..Thanks for the answer. urls.py is rather big.. thats the reason I did not post the whole thing..Which part of it would you like to see? the `urlpatterns`? please let me know. – JohnJ Aug 02 '13 at 19:49
  • Yeah, it's interesting to see what `url` is matched instead of `terms` one. Just create a gist. – alecxe Aug 02 '13 at 19:56
  • please see my gist here (with the `/?`): https://gist.github.com/anonymous/b7884eef9edbcdce66ae As you can see I am a bit confused myself since the template seems to work with other urls ending only with `/`. Strange! – JohnJ Aug 02 '13 at 20:08
  • I think `r'^(?P[-_\w]+)/$'` is cought instead. Can you double check it? – alecxe Aug 02 '13 at 20:11
  • You are absolutely correct `:(` How do I make sure that the other url is not caught? `:(` - why is the other url getting caught? `:(` Thanks so much for your help and advice on this. Been banging my head for the last couple of hours here! – JohnJ Aug 02 '13 at 20:15
  • I just did `url(r'^terms/conditions/$', TermsTemplateView.as_view(), name='terms'),` and that works.. no problems.. – JohnJ Aug 02 '13 at 20:17
  • 1
    Well, you should always be attentive to how your `urlpatterns` are ordered. Also, if possible, try to make your regexes more precise, exact..How to make sure sure that the other url is not caught - write tests :) – alecxe Aug 02 '13 at 20:21
  • Thanks alecxe for all your helpful advice on this issue. It has certainly taught me a lot! – JohnJ Aug 02 '13 at 20:41