简体   繁体   中英

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.

I am just getting started with working with python and flask I was under the impression that the error has to do with 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM