【问题标题】:How to access variables within html template url_for如何访问 html 模板 url_for 中的变量
【发布时间】:2018-06-24 06:06:21
【问题描述】:

我正在为我的 Devops 课程构建一个类似于 Netflix 的网站。我制作了一个 Python 字典列表(模拟电影)来定义我的电影,并希望用评论填充数据库(评级),以准备以 :filmid: :userid: :rating: 格式发送数据到推荐引擎。

我的索引页面是一个电影图片列表,每张图片下方都有一个评论表单的链接。我希望每个评论表单出现在不同的 url 上(/review/ID,其中 ID 以 oid 形式保存在模拟电影中)。为了做到这一点,我想访问 mockfilms.oid,然后将它传递给 view 函数来制作表单的 url。表单完成后,我想将此 ID 添加到 Ratings 数据库。这是我目前所拥有的:

索引:

{% extends "base.html" %}

{% block content %}
    <h1>Hello, {{ current_user.username }}! Welcome to our extensive video library:</h1>
    {% for film in mockfilms %}
    {% set ID = film.oid %}
    <div>
        <a href = {{ film.video }}>
            <img src = {{ film.image }} alt = "doh" style = "width:200px;height:200px;border:0;">
        </a>
    </div>
    <div>

        <a href={{ url_for('review', ID) }}"> ">Leave a review here!</a>
    {% endfor %}
{% endblock %}

路线:

@app.route('/review/<ID>', methods = ['GET', 'POST'])
@login_required
def review(ID):
    form = ReviewForm()
    if form.validate_on_submit():
        review = Ratings(User_id = current_user.id, Score_given = form.score.data, Film_id = ID)
        db.session.add(review)
        db.session.commit()
        flash('Thanks for your review')
        return redirect(url_for('index'))
    return render_template('review.html', title='Review Page', form=form)

运行时出现以下错误:

文件“/home/jc/Desktop/Lokal/DevopsAssig/microblog/Kilfinnan/lib/python3.5/site-packages/werkzeug/routing.py”,第 1768 行,在构建中 引发 BuildError(端点、值、方法、自身) werkzeug.routing.BuildError:无法为端点“审查”构建 url。您是否忘记指定值 ['ID']?

据此,我认为问题出在此模板中的 ID 变量上。我的搜索和学习使我相信索引模板中的 {% set %} 可以让我声明 ID 变量,然后在动态中使用它。

【问题讨论】:

  • 已解决问题 - @abigperson 提供的函数式语法。事实证明,我在模板的其他地方再次调用了 urlfor review,这就是导致错误的原因。

标签: python flask jinja2 werkzeug


【解决方案1】:

试试这个:

{% block content %}
    <h1>
        Hello, {{ current_user.username }}! 
        Welcome to our extensive video library:
    </h1>
    {% for film in mockfilms %}
    <div>
        <a href="{{ film.video }}">
            <img src="{{ film.image }}" alt="doh" style="width:200px;height:200px;border:0;" />
        </a>
    </div>
    <div>
        <a href="{{ url_for('review', ID=film.oid) }}">
            Leave a review here!
        </a>
    </div>
    {% endfor %}
{% endblock %}

最终您的解决方案非常接近,但是当您需要使用参数关键字将变量传递给url_for() 函数时,不需要使用Jinja set 命令。你仍然可以使用{% set ID = film.oid %} 来完成它,但这有点多余。

【讨论】:

  • 感谢您的回复!我已经按原样尝试过,但返回了相同的错误。
  • 看看这里:stackoverflow.com/questions/7478366/…。另一个回答者和我都回答了​​同一个问题......我没有看到任何其他错误并且无法测试/复制问题。抱歉,这没有帮助。
  • 已修复!我忘记了几天前(当我决定如何构建网站时)我在我的 base.html 文件中留下了对 Url_for(review) 的调用,似乎是那个调用而不是而不是导致问题的这个。非常感谢您向我保证问题出在其他地方 - 这是一个巨大的帮助。
【解决方案2】:

尝试在您的url_for 函数中提供key=value 参数。

类似的东西

<a href={{ url_for('review', ID=ID) }}"> ">Leave a review here!</a>

Flask 也有很好的文档,Flask docs

【讨论】:

  • 谢谢 Mike - 不幸的是返回了同样的错误。
猜你喜欢
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
  • 2021-07-23
  • 2017-12-28
  • 2021-05-25
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多