首页 > 解决方案 > Flask、Jinja2 和 WTForms - 所有 URL 的一种方法?

问题描述

我正在用 Flask 构建一个简单的网站,无论用户当前是哪个 url,用户登录表单都在右上角可用(html 表单在我的 base.html 模板中定义,该模板由所有其他模板扩展)。所以让它工作的一个选择,就是在每个@app.route() 方法中处理登录表单,但是它增加了很多冗余代码,看起来很难看,我正在寻找一种简化它的方法.

所以我的问题是:是否可以创建一种方法来处理我的应用程序中每个端点的登录表单?

这是我的登录表单的一些代码:

@app.route('/', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
    return redirect(url_for('home'))

form = LoginForm()
if form.validate_on_submit():
    user = User.query.filter_by(username=form.username.data).first()

    if user is None or not user.check_password(form.password.data):
        flash('Invalid username or password')
        return redirect(url_for('login'))

    login_user(user, remember=True)
    return redirect(url_for('home'))

return render_template('index.html', form=form)

这是渲染表单本身的屏幕截图:

https://i.stack.imgur.com/PXNSB.png

编辑:

到目前为止,我只想出了这个,但有没有更好的解决方案?(端点参数是将用户重定向回他登录的页面)

# method used in each URL for login purpose
def login_function(form, endpoint):
user = User.query.filter_by(username=form.username.data).first()

if user is None or not user.check_password(form.password.data):
    flash('Invalid username or password')
    return redirect(url_for(endpoint))

login_user(user, remember=True)
return redirect(url_for(endpoint))

在每个 URL 中访问该方法,如下所示:

login_form = LoginForm()
if login_form.validate_on_submit():
    return login_function(login_form, 'home')

标签: pythonwebflaskjinja2flask-wtforms

解决方案


您可以指定仅需要请求的路由,并在表单标头中的参数中POST指定该路由:action

form = """
  <form action='/form_post_route' method=['POST']>
     <input type='text' name='some_val'>
     <input type='submit'>Submit</input>
  </form>
"""
@app.route('/', methods=['GET'])
def home():
  return f'{form}\n<h1>Welcome</h1>'
@app.route('/some_other_route', methods=['GET'])
def other_route():
  return f'{form}\n<h1>Some data here</h1>'
@app.route('/last_route', methods=['GET'])
def last_route():
  return f'{form}\n<h1>Other data here</h1>'
@app.route('/form_post_route', methods=['POST'])
def get_user_data():
   param = flask.request.form['some_val']
   return flask.redirect('/')

推荐阅读