【发布时间】:2020-09-01 14:53:51
【问题描述】:
我正在学习 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>
然后,在 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")
这将返回内部服务器错误。有人知道为什么吗?
感谢您的宝贵时间!
【问题讨论】:
-
我回答你。如果这没有帮助,那么添加你得到的错误代码。另外,如果我使用 SQLAlchemy 解决方案,那么我可以使用 Vanilla SQLite 上传我的存储库(这将需要一些时间),因为无论如何我都打算这样做。