【问题标题】:Flask: one template does not extend base correctly烧瓶:一个模板没有正确扩展基地
【发布时间】:2017-04-30 16:40:28
【问题描述】:

我的一个模板 (offers_overview.html) 没有正确继承我的 base.html 模板。相反,它只输出 offers_overview.html 中给出的 html,而不输出 base.html 中的 html。

我有另一个模板 (customer_overview.html),其 html 与 offer_overview.html 完全相同,它很好地扩展了 base.html。这就是让我感到困惑的原因。 我做错了什么?

这是 offers_overview 我的 views.py 的样子:

@mod.route('/<username>/<int:inquiry_id>', methods=['GET', 'POST'])
@login_required
def show_offers_overview(username, inquiry_id):
 user = Customers.query.filter_by(cusername=username).first()
 inquiry = Inquiry.query.filter_by(iid=inquiry_id).first()

 if (user or inquiry) is None:
    flash('User %s or inquiry %s is not found.' % (username, inquiry_id))
    return redirect(url_for('user.user_overview', username=user.cusername))
 return render_template('offers_overview.html', inquiry=inquiry)

customer_overview 的views.py 如下所示:

@mod.route('/<username>', methods=['GET', 'POST'])
@login_required
def user_overview(username):
 user = Customers.query.filter_by(cusername=username).first()
 if user is None:
    flash('User %s not found.' % username)
    return redirect('/user/login')
 return render_template('customer_overview.html', customer=user)

这里是 offer_overview.html:

{% extends 'base.html' %}

{% block title %}Offers overview{% endblock title %}
{% block body %}
  <h2>Not important {{ inquiry.worktitle }}</h2>
  <br>
  <h3>Future</h3>
  <br>
  <h3>Present</h3>
  <br>
  <h3>Past</h3>
{% endblock body %}

这与 customer_overview.html 中的代码完全相同,只是将查询更改为客户。

我尝试将要在 show_offers_overview() 中呈现的模板更改为 customer_overview。结果是子模板没有继承基础模板。

更新 这就是模板的结构:

|project\
   |app\
      |templates\
         |--base.html
      |user\
         |templates\
            |--customer_overview.html
            |--offers_overview.html
         |--__init__.py
         |--views.py

【问题讨论】:

  • 这3个html模板都在同一个目录下吗?
  • 不,base 在“全局”模板目录中。其他的在蓝图特定的模板目录中。

标签: python html templates flask


【解决方案1】:

我发现在我的 base.html 中,我的代码使更深的页面不继承静态文件 off-canvas.css。例如页面

localhost:5000/user/myusername

可以,但不行

localhost:5000/user/myusername/123

在我引用 css 静态文件的 base.html 中看起来像这样:

<link href="../static/off-canvas.css" rel="stylesheet">

我必须在 base.html 中更改为 url_for,请参见此处:

<link href="{{url_for('static', filename='off-canvas.css')}}" rel="stylesheet">

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2019-02-10
    • 1970-01-01
    相关资源
    最近更新 更多