【发布时间】:2018-10-03 12:12:12
【问题描述】:
我正在尝试使用 url 名称将自定义 arg 发送到我的 TemplateView。
网址.py
urlpatterns = [
url(r'^$', index.as_view(template_name='index.html', gotodiv='whatis'), name='whatis'),
url(r'^$', index.as_view(template_name='index.html', gotodiv='whybepartofit'), name='whybepartofit')
]
Views.py
class index(TemplateView):
template_name = "index.html"
gotodiv = ''
def get_context_data(self, **kwargs):
context = super(index, self).get_context_data(**kwargs)
context['gotodiv'] = self.gotodiv
return context
模板
<li>
<a href="{% url 'whatis' %}">What is it?</a>
</li>
<li>
<a href="{% url 'whybepartofit' %}">Why be part of it?</a>
</li>
问题在于它不关心 url 名称,因为模式在两种情况下都匹配。所以它总是进入第一个模式(在这个例子中,那个带有“whatis”gotodiv arg. 有没有办法配置模式,让它只关心名称?
谢谢
【问题讨论】:
-
顺便说一句,最好将基于类的视图命名为
Index。如果你使用小写的index,它在其他 Django 用户看来就像一个基于函数的视图。
标签: django django-templates django-views