Flask - jinja2.exceptions.UndefinedError

时间:2017-05-24 18:32:32

标签: python flask jinja2

我正在使用python flask在简单的网络上工作。我想做的其实很简单:我想输出" aaa"在按钮单击的网页上。这是我的HTML模板(addUser.html):

<html>
<head>
    <title></title>
</head>

<body>
    <form action="{{ url_for('route_add_user') }}" method="post" name="route_add_user">
        {{ form_add_user.hidden_tag() }}
        <h1>Create New User</h1>

        Username: <input type="text" name="username_add" placeholder="Username">

        <br>
        Password: <input type="password" name="password_add" placeholder="Password">

        <br>
        Nama: <input type="text" name="name_add" placeholder="Name">

        <br>
        E-mail: <input type="text" name="email_add" placeholder="E-mail">

        <br>
        Role:<select name="role_add">
            <option>Admin</option>
            <option selected="selected">User</option>
        </select>

        <br><br>
        <input type="submit" value="Add New User">
    </form>
</body>

这一个是观点(views.py):

@app.route('/route_add_user', methods=['GET', 'POST'])
def route_add_user():
    iTemp_1 = 1
    form_add_user = AddUser()
    if form_add_user.validate_on_submit():
        if iTemp_1 == 1:
            return "aaa"
    return render_template('addUser.html', form_add_user = form_add_user)

我收到了错误:

jinja2.exceptions.UndefinedError: 'form_add_user' is undefined

我很确定我已经定义了&#34; form_add_user&#34;。我错过了什么?

1 个答案:

答案 0 :(得分:0)

@app.route('/route_add_user', methods=['GET', 'POST'])
def route_add_user():
    iTemp_1 = 1
    form_add_user = AddUser()
    if form_add_user.validate_on_submit():
        if iTemp_1 == 1:
            flash('aaa')
    return render_template('addUser.html', form_add_user = form_add_user)