[英]when I try to run my flask/python application it gives me the error: A secret key is required to use CSRF
when I try to run my flask/python application it gives me the error: A secret key is required to use CSRF.当我尝试运行我的烧瓶/python 应用程序时,它给了我错误:使用 CSRF 需要密钥。
I am just getting started with working with python and flask I was under the impression that the error has to do with wtforms.我刚刚开始使用 python 和 flask 我的印象是该错误与 wtforms 有关。
from flask import Flask, render_template, url_for
from forms import RegistrationForm, LoginForm
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secretkey'
posts = [
{
'author': 'John Doe',
'title': 'post 1',
'content': 'first post content',
'date': 'March 26, 2020'
},
{
'author': 'Other Person',
'title': 'post 2',
'content': 'second post content' ,
'date': 'March 26, 2020'
}
]
app = Flask(__name__)
@app.route('/')
@app.route('/home')
def home():
return render_template('home.html', posts=posts)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/register')
def register():
form = RegistrationForm()
return render_template('register.html', title = 'Register', form=form)
@app.route('/login')
def login():
form = LoginForm()
return render_template('login.html', title = 'Login', form=form)
if __name__ == '__main__':
app.run(debug=True)
You have recreated the app instance here您已在此处重新创建了应用程序实例
app = Flask(__name__)
Remove the second instance of that line and it should work.删除该行的第二个实例,它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.