【问题标题】:I have set a secret key, but keep getting the error 'RuntimeError: A secret key is required to use CSRF.'我设置了一个密钥,但不断收到错误“RuntimeError: A secret key is required to use CSRF.”
【发布时间】:2020-10-15 13:05:09
【问题描述】:

我对 Flask 很陌生,在运行它时遇到了一些麻烦。我设置了一个密钥,但不断收到同样的错误。任何帮助将不胜感激。

这是我的代码:

from flask import Flask, redirect, url_for, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField
import os

app = Flask(__name__)
SECRET_KEY = os.urandom(32)
app.config['SECRET KEY'] = 'secrfgdgret'

class LoginForm(FlaskForm):
    username = StringField('username')
    password = PasswordField('password')

@app.route('/')
def login():
    form = LoginForm()
    return render_template('login.html', form=form)

@app.route('/register')
def register():
    return render_template('register.html',)





@app.route('/home')
def home():
    return render_template('index.html',)


@app.route('/drive')
def drive():
    return render_template('drive.html',)

@app.route('/deliver')
def deliver():
    return render_template('deliver.html',)



if __name__ == '__main__':
    app.run(debug=True)

【问题讨论】:

  • app.config['SECRET KEY'] = ... 中添加下划线,使其变为app.config['SECRET_KEY']

标签: python flask flask-wtforms


【解决方案1】:

正如 Thomas Weller 所建议的,在 'SECRET KEY' 中添加下划线-> 'SECRET_KEY' in app.config['SECRET_KEY']

【讨论】:

  • 如果出现简单的拼写错误,我们更愿意关闭问题。甚至还有一个具体的票数接近的原因提到了拼写错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 2020-03-09
  • 2019-01-14
相关资源
最近更新 更多