Django 中的导航 [英] Navigation in django
问题描述
我刚刚在 django 中完成了我的第一个小网络应用程序,我喜欢它.我即将开始将旧的生产 PHP 站点转换为 django,并且作为其模板的一部分,有一个导航栏.
I've just done my first little webapp in django and I love it. I'm about to start on converting an old production PHP site into django and as part its template, there is a navigation bar.
在 PHP 中,我在模板代码中根据当前 URL 检查每个导航选项的 URL,如果它们对齐则应用 CSS 类.乱七八糟的.
In PHP, I check each nav option's URL against the current URL, in the template code and apply a CSS class if they line up. It's horrendously messy.
django 是否有更好的方法或处理模板中代码的好方法?
Is there something better for django or a good way of handling the code in the template?
首先,我将如何获取当前 URL?
To start, how would I go about getting the current URL?
推荐答案
我使用模板继承来自定义导航.例如:
I use template inheritance to customize navigation. For example:
base.html
<html>
<head>...</head>
<body>
...
{% block nav %}
<ul id="nav">
<li>{% block nav-home %}<a href="{% url 'home' %}">Home</a>{% endblock %}</li>
<li>{% block nav-about %}<a href="{% url 'about' %}">About</a>{% endblock %}</li>
<li>{% block nav-contact %}<a href="{% url 'contact' %}">Contact</a>{% endblock %}</li>
</ul>
{% endblock %}
...
</body>
</html>
关于.html
{% extends "base.html" %}
{% block nav-about %}<strong class="nav-active">About</strong>{% endblock %}
这篇关于Django 中的导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!