我一直在尝试使用Flask、WTForms和Firebase数据库构建Web应用程序,但是我不断收到错误消息:“KeyError:'A secret key is required to use CSRF。'”,而我不知道如何解决它。 以下是我的代码:
from flask import Flask, render_template, request
from firebase import firebase
from flask_wtf import FlaskForm
from flask_wtf.csrf import CSRFProtect, CSRFError
from wtforms import DateField, StringField, TextAreaField
from wtforms.validators import DataRequired
from wtforms_components import TimeField
app = Flask(__name__)
csrf = CSRFProtect(app)
firebase = firebase.FirebaseApplication("https://uhungry-f9563.firebaseio.com", None)
class myForm(FlaskForm):
event = StringField("event", validators=[DataRequired()])
location = StringField("location", validators=[DataRequired()])
startDay = DateField("startDay", validators=[DataRequired()])
startTime = TimeField("startTime", validators=[DataRequired()])
endDay = DateField("endDay", validators=[DataRequired()])
endTime = TimeField("endTime", validators=[DataRequired()])
details = TextAreaField("details", validators=[DataRequired()])
count = 0
@app.route('/', methods=['GET' , 'POST'])
def home():
form = myForm()
if form.validate_on_submit():
global count
count += 1
putData = {'Event': form.event.data, 'Location': form.location.data, 'startDay': form.startDay.data, 'startTime': form.startTime.data,'endDay': form.endDay.data, 'endTime': form.endTime.data, 'Details': form.details.data}
firebase.put('/events', 'event' + str(count), putData)
return render_template("trial.html")
return render_template("home.html")
if __name__ == '__main__':
app.run(debug=True)