在 django 中导航 [英] Navigation in django

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

问题描述

我刚刚在 django 中完成了我的第一个小 webapp,我喜欢它.我即将开始将一个旧的生产 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屋!

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