【问题标题】:Flask - Templates, Routes and HTMLFlask - 模板、路由和 HTML
【发布时间】:2020-07-02 04:10:59
【问题描述】:

我正在使用 Flask 设计一个 web 应用程序,并开始创建一个模板来作为我其他页面的基础。

对于我的导航栏,我希望在当前选定页面的按钮上显示一个彩色指示器。 Flask 模板中是否有某种方式:

{% if "route == '/'": %}
  <a class="navbar-item has-background-primary" href='/'> Home </a>
{% else: %}
   <a class="navbar-item" href='/'> Home </a>
{% endif %}

我希望减少使用此代码的次数,并实际使用模板功能,而不是在我将设计的每个页面中都有导航栏的代码。

【问题讨论】:

    标签: flask


    【解决方案1】:

    我通过在路由中传递变量来做到这一点,例如将“is_home”设置为随机值(“是”)。然后,如果定义了变量 - 显示带有背景的按钮(因为它将在该视图/路线上),否则显示空白按钮。

    @app.route('/downloads')
    def download_links():
        return render_template('download.html', is_downloads='Yes')
    
    
    
    
        <div id="navigationBar" class="navbar-menu">
                  <div class="navbar-start">
                   
                   
                    {% if is_home is not defined %}
                    
                    <a class="navbar-item" href='/'>
                    {% else %}
                    <a class="navbar-item has-background-success-light" href='/'>
                    
                    {% endif %}
    
                    Home
                  </a>
                  {% if is_downloads is not defined %}
                  <a class="navbar-item" href='/download'>
                
                  {% else %}
                  <a class="navbar-item has-background-success-light" href='/download'>
                  {% endif %}
              
                      Downloads
                    </a>
              
                </div>
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      相关资源
      最近更新 更多