【问题标题】:Is there a way to hide a button on every page except Home page of a site in Django?有没有办法在除 Django 中网站的主页之外的每个页面上隐藏一个按钮?
【发布时间】:2020-08-19 22:12:48
【问题描述】:
<div class="page-header">
             {% if user.is_authenticated %}
                <a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
             {% endif %}
             <h1><a href="/">Django Girls Blog</a></h1>
             {% if user.is_anonymous %}
             <h4><a href="/login">LogIn</a></h4>
             {% endif %}
             {% if user.is_authenticated %}
              <a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
             {% endif %}
             {% if user.is_authenticated %}
             <h3><a href="/user">{{ user }}</a></h3>
             <h4><a href="/logout">LogOut></a></h4>
             {% endif %}

【问题讨论】:

  • 只将代码放在主页上。
  • 您可以在主页视图中将show_button 添加到context 字典中,然后在模板中执行{% if show_button %}Button{% endif %} 之类的操作。如果您需要更多帮助,请在主页上显示您的视图,并说明您要隐藏哪个按钮。
  • @SergeyPugach 我将我的主页设置为所有其他页面的 base.html,因此它反映在我拥有的每个页面上。
  • 你可以有一个 homepage.html 与你的逻辑扩展 base.html
  • @SergeyPugach 是的,可以选择其中之一,谢谢。

标签: django python-3.x django-templates


【解决方案1】:

当然。检查当前页面是否是主页,如果不是,则使用 css 隐藏按钮。像这样:

<button {% if request.path != '/' %} style="display: none;" {% endif %}>My Button</button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多