我正在尝试使用flask docs方法将图像上载到我的flask应用程序,但此错误不断出现:
400 Bad Request, CSRF Token is missing.
以下是视图功能:
# file uploading (avatar)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@login_required
@app.route('/upload_avatar', methods=['POST', 'GET'])
def upload_avatar():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(url_for('user'))
file = request.files.get('file', False)
if file.filename == '':
flash('No selected file')
return redirect(url_for('user'))
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save = (os.path.join(app.config['UPLOAD_FOLDER'], current_user.username))
return redirect(url_for('uploaded_file', filename=filename))
return render_template('upload_avatar.html')
@login_required
@app.route('/uploaded_file')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
这是模板:
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
我有csrf
csrf = CsrfProtect()
csrf.init_app(app)
UPLOAD_FOLDER = '/mnt/d/work/mysnsite/app/uploads/'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
CKEDITOR_ENABLE_CSRF = True
WTF_CSRF_ENABLED = False
WTF_CSRF_SECRET_KEY = 'secretkey'
app.config['SECRET_KEY'] = 'secretkey2'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
我已经尝试添加了{{ form.csrf_token() }}
和{{ form.hidden_tag() }}
,但它似乎不起作用,因为我的视图函数中没有初始化表单
目前没有回答
相关问题 更多 >
编程相关推荐