我正在学习 Python 并使用 Flask 开发一个应用程序,其中一个功能是用户可以上传个人资料图片,并且该图像应该保存在数据库中。
我有一个用户可以上传文件的表单,如下所示:
<form action="/myprofile" method="post" enctype="multipart/form-data">
<div class="form-group">
<input class="form-control" name="picture" placeholder="Picture" type="file">
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
然后,在 application.py 文件中我像这样处理它:
@app.route("/myprofile", methods=["GET", "POST"])
@login_required
def myprofile():
if request.method == "POST":
db.execute("UPDATE users SET role = :role, picture = :picture, linkedin = :linkedin WHERE id = :user_id", role = request.form.get("role"), picture = request.files("picture"), linkedin = request.form.get("linkedin"), user_id = session["user_id"])
return render_template("home.html")
else:
return render_template("myprofile.html")
Run Code Online (Sandbox Code Playgroud)
这将返回内部服务器错误。有谁知道为什么吗?
谢谢你的时间!