Python:TypeError:redirect()得到了一个意外的关键字参数'错误'

时间:2017-01-18 11:20:49

标签: python flask

我正在使用Flask,当我填写其中一个表单部件时,我试图出现错误。当我测试它时(填写所有表单部分) ,它给了我错误:" TypeError:redirect()得到了一个意外的关键字参数'错误'"。我做错了什么?

Flask代码:

@app.route('/crudcourse', methods = ['POST'])
def crudcourse():
db = get_db()
error = None

button = request.form['submitcourse']

coursename = request.form['coursename']
description = request.form['description']

if coursename != None or description != None:
    error = 'Vul alle velden in aub'
else:
    if button == 'Toevoegen':
        db.execute('insert into courses (coursename, description) 
        values(?, ?)', coursename, description)
        db.commit()
        flash('Gegevens sucessvol toegevoegd')

    elif button == 'Updaten':
        db.execute('update courses set coursename=?, description=? where
        coursename=? or description=?',
        coursename, description, coursename, description)
        db.commit()
        flash('Gegevens sucessvol geupdate')

    elif button == 'Verwijderen':
        db.execute('delete from courses where coursename=? or
        description=?', coursename, description)
        db.commit()
        flash('Gegevens sucessvol verwijderd')  

return redirect('/intranet/richtingen.html', error=error)

HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Home</title>
    <link rel="stylesheet" type="text/css" href="templates/style.css">
</head>

<body>

{% if error %}
<p class=error><strong>Error:</strong> {{ error }}
{% endif %}

{% for message in get_flashed_messages() %}
<div class=flash>{{ message }}</div>
{% endfor %}

    <div id="courses">
        <form action="/crudcourse" method="post">
            <p>Naam<input type="text" name="coursename"></p>
            <p>Beschrijving
                <textarea name="description" cols="50" rows="4">
                </textarea></p>

            <input type="submit" name="submitcourse" value="Toevoegen">
            </input>
            <input type="submit" name="submitcourse" value="Updaten">
            </input>
            <input type="submit" name="submitcourse" value="Verwijderen">
            </input>
        </form>
    </div>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

我猜测你引用的重定向方法是烧瓶库flask.redirect提供的方法。可以像这样导入:

from flask import redirect

如果这是真的,则此重定向方法不会采用错误参数,因为错误消息是TypeError: redirect() got an unexpected keyword argument 'error'

您想要做的就是将任何潜在的错误传递到/intranet/richtingen.html页面,如果是这样的话 - 我建议您在URL的查询字符串中这样做,如下所示:

return redirect('/intranet/richtingen.html?error=%s' % error)