我正在尝试将Flask应用程序导入到守护程序中。我将大部分代码导入了routes.py
中,并根据蓝图进行了修改。运行守护程序时,我得到KeyError: 'A secret key is required to use CSRF'
。如果我更改为MyForm(csrf_enabled=False)
,则可以正常工作。 app.config['SECRET_KEY']
为什么不起作用?
routes.py
import re, csv, os, logging
from flask import Flask, render_template, flash, Blueprint
from flask_wtf import FlaskForm
from wtforms import StringField
from pxeadmin.daemon.implementation import PxeAdmin
LOG = logging.getLogger(__name__)
# Make sure this matches your repo's template directory
TEMPLATE_FOLDER = 'templates'
DAEMON_BLUEPRINT = Blueprint('daemon.routes.blueprint', __name__, template_folder=TEMPLATE_FOLDER)
app = Flask(__name__)
app.config["SECRET_KEY"] = "xc7rx86xafxf0ex8cxd2xb6flxfel4stLxd5xdbx18Sx1e"
app.register_blueprint(DAEMON_BLUEPRINT)
class MyForm(FlaskForm):
"""Index page form class"""
name = StringField('Full Name:')
uname = StringField('Username:')
asset = StringField('Asset Tag:')
mac = StringField('MAC Address:')
mac_remove = StringField('MAC Address:')
@DAEMON_BLUEPRINT.route('/', methods=['GET', 'POST'])
def index():
"""main page/form for adding/viewing/removing entries from sys_map.csv"""
pxe_form = MyForm()
mac_remove_i = pxe_form.data['mac_remove']
name_i = pxe_form.data['name']
uname_i = pxe_form.data['uname']
asset_i = pxe_form.data['asset']
mac_i = pxe_form.data['mac']
if pxe_form.validate_on_submit() and pxe_form.data['mac_remove']:
remove_one(mac_remove_i)
elif pxe_form.validate_on_submit() and name_i and uname_i and \
asset_i and mac_i:
add_to_list(name_i, uname_i, asset_i, mac_i)
content = the_list()
return render_template('index.html', form=pxe_form, contents=content)
__main__.py
from pxeadmin.daemon.implementation import PxeAdmin
from pxeadmin.daemon.routes import DAEMON_BLUEPRINT, register_error_handlers
def _main():
# Instantiate the singleton. We must do this first to get an instance of the Flask app.
daemon_app = PxeAdmin()
# Set up the Flask error handlers and registering the daemon blueprint so that our HTTP endpoints work.
register_error_handlers(daemon_app.flask_app)
daemon_app.flask_app.register_blueprint(DAEMON_BLUEPRINT)
# Run the implementation.
daemon_app.run()
if __name__ == '__main__':
_main()
implementation.py
from cmdlineutil.daemon import Daemon
from cmdlineutil.property_manager import PropertyManager, Property
from pxeadmin.daemon.dependencies import build_dependencies
LOG = logging.getLogger(__name__)
class PxeAdmin(Daemon):
"""Entry point for python daemon applications.
name = 'pxe-admin-app'
def __init__(self):
super(PxeAdmin, self).__init__(PxeAdmin.name)
我还正确地包含了表单隐藏标签。
<form action="/" method="POST">
{{ form.hidden_tag() }}
{{ form.name.label }} {{ form.name(size=20) }} <br>
.....
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
答案 0 :(得分:0)
您已经在app
中创建了一个routes.py
,但它似乎与您正在服务的daemon_app.flask_app
实例无关。您对Flask
实例所做的任何配置都不适用于单独的Flask
实例。将配置应用到daemon_app.flask_app.config
。