简体   繁体   English

当我尝试运行我的烧瓶/python 应用程序时,它给了我错误: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

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.

相关问题 您如何解决错误 KeyError: 'A secret key is required to use CSRF.' 在烧瓶应用程序中使用 wtform 时? - How do you solve the error KeyError: 'A secret key is required to use CSRF.' when using a wtform in flask application? 错误 使用 CSRF 需要密钥。 蟒蛇/烧瓶 - Erro A secret key is required to use CSRF. Python/Flask RuntimeError:使用 CSRF 需要密钥。 我已经使用了一个密钥,但它似乎仍然给出了错误 - RuntimeError: A secret key is required to use CSRF. I have used a secret key but it still seems to give the error 我设置了一个密钥,但不断收到错误“RuntimeError:使用 CSRF 需要密钥”。 - I have set a secret key, but keep getting the error 'RuntimeError: A secret key is required to use CSRF.' Flask 表单 + 蓝图:KeyError:“使用 CSRF 需要密钥” - Flask Forms + Blueprints: KeyError: 'A secret key is required to use CSRF' 为什么我尝试运行我的秘密令牌时出现错误 - Why is there an error when I try to run my secret token 当我尝试导入 pandas 时,我的 python 控制台总是给我错误 - my python console always gives me error when i try to import pandas 当我尝试连接-python时,套接字给我错误 - Socket gives me error when i try to connect -python 当 csrf_enabled 为 True 时,Flask-WTF 抛出错误(设置了 SECRET_KEY) - Flask-WTF throws error when csrf_enabled is True (SECRET_KEY is set) Pytest尝试运行测试时给我一个错误 - Pytest gives me an error when try to run a test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM