【发布时间】:2013-08-04 03:01:33
【问题描述】:
我有一个由 ListViews、TemplateViews 等组成的 django 应用程序。 所以,我只是像这样添加了一个小的模板视图:
#views.py
class TermsTemplateView(TemplateView):
template_name = "terms.html"
#urls.py
url(r'^terms/$', TermsTemplateView.as_view(), name='terms'),
在 terms.html 中,我用于链接:
<a href="{% url 'terms' %}">Terms & Conditions</a>
出于某种奇怪的原因,我在 localhost/terms 上不断收到 404,如下所示:
404: No <model_name> found matching the query
我很困惑为什么会突然发生这种情况。我为"about", "thanks", "contact" 页面设置了相同的设置,它们似乎可以毫无问题地显示它。
..最糟糕的是,如果我像这样修改 urls.py:
url(r'^/terms/$', TermsTemplateView.as_view(), name='terms'),
然后转到http://127.0.0.1:8000//terms/ - 页面似乎在那里.. 我很惊讶为什么会这样:(
任何帮助都会启发我!
【问题讨论】:
-
正则表达式末尾的
/怎么样 - 如果您删除它并尝试localhost/terms会怎样?如果没有帮助,请出示您的urls.py。 -
天哪!你是对的。当我从 urls.py 中取出
/并转到localhost/terms时,它可以工作。为什么这样??我现在很困惑。另外,请将其作为答案 - 我会接受。 -
太棒了!请看答案。
标签: django django-templates django-urls