所以现在我硬编码到url,如果你移动端点,这有点烦人。这是我的导航栏项目的当前设置。
# in base.html
{% include 'components/navbar/nav-item.html' with title='Event Manager' url='/eventmanager/' %}
# in components/navbar/nav-item.html
<li>
<a href="{{ url }}">{{ title }}</a>
</li>
看看我现在如何使用url?
我现在想要的是:
{% include 'components/navbar/link.html' with title='Event Manager' url={% url 'event_manager:index' %} %}
但显然,这是无效的语法。我该怎么做?
如果不可能,我该如何创建一个应用程序,以某种方式创建一个视图,在该视图中我可以传递带有上下文变量的所有URL?理论上,这听起来很容易,但我必须以某种方式将这种观点插入其他所有观点中。
您可以将其作为variable
而不是django的url tag
传递,使用如下:
{% url 'event_manager:index' as myurl %}
{% include 'components/navbar/link.html' with myurl %}
现在您可以将它传递给include
标签。
修复它的解决方案:
# navbar.html
{% include 'components/navbar/nav-item.html' with title='Event Manager' to_url='event_manager:index' %}
# nav-link.html
{% url to_url as myurl %}
<li>
<a href="{{ myurl }}">{{ title }}</a>
</li>