繁体   English   中英

flask render_template 传递的变量未在模板中使用

[英]flask render_template passed variable not being used in the template

我目前正在做一个项目,我遇到了 flask 和 render_template 的问题。 当我将变量传递给 render_template 时,它实际上并没有传递给模板。 我已经尝试将字符串放入变量和其他一些东西中,但到目前为止没有任何效果。

这是我要返回的 function:

return render_template("sell.html", error="Invalid Ticker")

这是“sell.html”:

{% extends "layout.html" %}

{% block title %}Sell{% endblock %}

{% block main %}
    <h1>Sell a Stock!</h1>
    {% if error is defined %}
        <div class="alert alert-danger">
            {{error}}
        </div>
    {% endif %}
    {% if success is defined %}
    <div class="alert alert-success">
            {{success}}
        </div>
    {% endif %}
    <form action="/sell" method="post">
        <div class="form-group">
            <label for="ticker">Ticker:</label>
            <input type="text" class="form-control" id="ticker" name="ticker">
        </div>
        <div class="form-group">
            <label for="buynum">Shares:</label>
            <input type="number" class="form-control" id="sellnum" name="sellnum">
        </div>
        <button type="submit" class="btn btn-primary">Sell</button>
    </form>

{% endblock %}

问题不在 render_template function 中,它工作正常。 问题是error is defined在 jinja2 中不存在。

只需尝试一个更简单的 html 示例来测试:

{% block title %}Sell{% endblock %}

{% block main %}
<p> {{error}} </p>
{% endblock %}

所以改变:

{% if error is defined %}

and 

{% if success is defined %}

{% if error %}

{% if sucess %}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM