KeyError: 'A secret key is required to use CSRF.'

林修雅
2023-12-01
KeyError
KeyError: 'A secret key is required to use CSRF.'

Traceback (most recent call last)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/huanglianggu/Desktop/artcms_project/views.py", line 11, in login
form = LoginForm()
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/wtforms/form.py", line 212, in __call__
return type.__call__(cls, *args, **kwargs)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask_wtf/form.py", line 88, in __init__
super(FlaskForm, self).__init__(formdata=formdata, **kwargs)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/wtforms/form.py", line 278, in __init__
self.process(formdata, obj, data=data, **kwargs)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/wtforms/form.py", line 132, in process
field.process(formdata)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/wtforms/csrf/core.py", line 43, in process
self.current_token = self.csrf_impl.generate_csrf_token(self)
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask_wtf/csrf.py", line 134, in generate_csrf_token
token_key=self.meta.csrf_field_name
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask_wtf/csrf.py", line 35, in generate_csrf
message='A secret key is required to use CSRF.'
File "/Users/huanglianggu/Desktop/artcms_project/venv/lib/python2.7/site-packages/flask_wtf/csrf.py", line 121, in _get_config
raise KeyError(message)
KeyError: 'A secret key is required to use CSRF.'
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object

解决办法:

在/Users/huanglianggu/Desktop/artcms_project/views.py

添加

app.config["SECRET_KEY"] = "12345678"
# coding:utf8
from flask import Flask, render_template, redirect
from forms import LoginForm

app = Flask(__name__)
app.config["SECRET_KEY"] = "12345678"

 

 

 

 类似资料: