导航在django [英] Navigation in django
问题描述
在PHP中,我查看每个导航选项的针对当前URL的URL,在模板代码中并应用CSS类,如果它们排队。这是可怕的凌乱。
有没有更好的django或一个很好的方式来处理模板中的代码?
要开始,我如何获取当前的URL?
我使用模板继承自定义导航。例如:
base.html
< html>
< head> ...< / head>
< body>
...
{%block nav%}
< ul id =nav>
< li> {%block nav-home%}< a href ={%url home%}>首页< / a> {%endblock%}< / li>
< li> {%block nav-about%}< a href ={%url about%}>关于< / a> {%endblock%}< / li>
< li> {%block nav-contact%}< a href ={%url contact%}>联系人< / a> {%endblock%}< / li>
< / ul>
{%endblock%}
...
< / body>
< / html>
about.html
{%extendsbase.html%}
{%block nav-about%}< strong class =nav-active>关于< / strong> { %endblock%}
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.
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.
Is there something better for django or a good way of handling the code in the template?
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>
about.html
{% extends "base.html" %}
{% block nav-about %}<strong class="nav-active">About</strong>{% endblock %}
这篇关于导航在django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!