导航在django [英] Navigation in django

查看:125
本文介绍了导航在django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在django做了我的第一个小webapp,我喜欢它。我将开始将一个旧的生产PHP站点转换为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆