Django:多个菜单

发布于 2024-11-10 06:56:39 字数 175 浏览 1 评论 0原文

我的基于 django 的网站将有 3 个独立的菜单。第一项是:联系、关于、披露。第二个将包含:条款和条件、隐私政策、版权。主菜单的项目有:Home、link1、link2、link2……前两个菜单的项目是固定的,最后一个菜单的项目可能会改变。由于我将在模板中使用 forloop,因此创建这些菜单的最佳方法是什么。网页只有标题和内容。

My django based website will have 3 seperate menus. The items of first one are: contact, about, disclosures. The second one will have: terms and condtions, privacy policy, copyright. And items of main menu are: Home, link1, link2, link2.... The first two menus will have fixed items, and the items of last one may change. As I will be using forloop in the template, what is the best approach for creating those menus. The webpages will have just a title and content.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一刻暧昧 2024-11-17 06:56:39

我喜欢使用 包含模板标签动态菜单。

my-app/templatetags/myappmenu.py 中,我有类似的内容:

from django import template
register = template.Library()
@register.inclusion_tag('my-app/menu.html')
def myappmenu():
    return [("label1", "link1"), ("label2", "link2")]

然后,在模板中,您可以循环遍历项目并以您想要的格式生成菜单(

;

    等)。

如果需要使菜单中的项目有条件地显示,可以通过检查模板标签中的权限将其添加到列表中;只需将请求或用户对象作为参数传递给模板标记函数。

I like to use inclusion template tags for dynamic menus.

In my-app/templatetags/myappmenu.py, I have something like:

from django import template
register = template.Library()
@register.inclusion_tag('my-app/menu.html')
def myappmenu():
    return [("label1", "link1"), ("label2", "link2")]

Then, in your template you can loop over the items and produce the menu in the format you desire (<p>, <ul>, etc.).

If you need to make items in the menu appear conditionally, you can add them to the list by checking permissions in the template tag; just pass the request or user object as argument to the template tag function.

不喜欢何必死缠烂打 2024-11-17 06:56:39

您可以保持DRY,只使用django-menuware< /强>。它还支持嵌套菜单

安装:

pip install django-menuware
# Add `menuware` to your settings.py**
# Add `MENUWARE_MENU` to your settings.py:**

设置:

MENUWARE_MENU = {
    "RIGHT_NAV_MENU": [
        {   # Show `Login` to `unauthenticated` users ONLY
            "name": "Login",
            "url": "/login/",
            "render_for_unauthenticated": True,
        },
        {   # Show `Logout` to `authenticated` users ONLY
            "name": "Logout",
            "url": "/logout/",
            "render_for_authenticated": True,
        },
        {   # Show `Search` to all users
            "name": "Search",
            "url": "/search/",
            "render_for_unauthenticated": True,
            "render_for_authenticated": True,
        },
    ],
    "LEFT_NAV_MENU": [
        {   # Show `Admin` to `superuser` ONLY
            "name": "Admin",
            "url": "admin:index", # Reversible
            "render_for_authenticated": True,
            "render_for_superuser": True,
        },
       {   # Show `Comment Admin` to `staff` users ONLY
            "name": "Comment Admin",
            "url": "/review/admin/",
            "render_for_authenticated": True,
            "render_for_staff": True,
        },
    ]

使用:

<!-- base.html -->
{% load menuware %}

<!DOCTYPE html>
<html>
    <head><title>Django Menuware</title></head>
    <body>
        <!-- NAV BAR Start -->
        {% get_menu "LEFT_NAV_MENU" as left_menu %}
        <div style="float:left;">
            {% for item in left_menu %}
                <li class="{% if item.selected %} active {% endif %}">
                    <a href="{{item.url}}">{{item.name}}</a>
                </li>
            {% endfor %}
        </div>

        {% get_menu "RIGHT_NAV_MENU" as right_menu %}
        <div style="float:right;">
            {% for item in right_menu %}
                <li class="{% if item.selected %} active {% endif %}">
                    <a href="{{item.url}}">{{item.name}}</a>
                </li>
            {% endfor %}
        </div>
        <!-- NAV BAR End -->
    </body>
</html>

至少,您需要查看其 Github README 页面,然后再推出您自己的页面。

You can stay DRY and just use django-menuware. It supports nested menus as well.

Install:

pip install django-menuware
# Add `menuware` to your settings.py**
# Add `MENUWARE_MENU` to your settings.py:**

Settings:

MENUWARE_MENU = {
    "RIGHT_NAV_MENU": [
        {   # Show `Login` to `unauthenticated` users ONLY
            "name": "Login",
            "url": "/login/",
            "render_for_unauthenticated": True,
        },
        {   # Show `Logout` to `authenticated` users ONLY
            "name": "Logout",
            "url": "/logout/",
            "render_for_authenticated": True,
        },
        {   # Show `Search` to all users
            "name": "Search",
            "url": "/search/",
            "render_for_unauthenticated": True,
            "render_for_authenticated": True,
        },
    ],
    "LEFT_NAV_MENU": [
        {   # Show `Admin` to `superuser` ONLY
            "name": "Admin",
            "url": "admin:index", # Reversible
            "render_for_authenticated": True,
            "render_for_superuser": True,
        },
       {   # Show `Comment Admin` to `staff` users ONLY
            "name": "Comment Admin",
            "url": "/review/admin/",
            "render_for_authenticated": True,
            "render_for_staff": True,
        },
    ]

Usage:

<!-- base.html -->
{% load menuware %}

<!DOCTYPE html>
<html>
    <head><title>Django Menuware</title></head>
    <body>
        <!-- NAV BAR Start -->
        {% get_menu "LEFT_NAV_MENU" as left_menu %}
        <div style="float:left;">
            {% for item in left_menu %}
                <li class="{% if item.selected %} active {% endif %}">
                    <a href="{{item.url}}">{{item.name}}</a>
                </li>
            {% endfor %}
        </div>

        {% get_menu "RIGHT_NAV_MENU" as right_menu %}
        <div style="float:right;">
            {% for item in right_menu %}
                <li class="{% if item.selected %} active {% endif %}">
                    <a href="{{item.url}}">{{item.name}}</a>
                </li>
            {% endfor %}
        </div>
        <!-- NAV BAR End -->
    </body>
</html>

Minimally, you'd want to look at its Github README page before rolling your own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文