个人资料图片不会在 python/flask 后上传
profile picture won't upload in python/flask
我是新来的,也是初学者,所以我会很感激你 help.I 我正在尝试在 Flask 中做博客应用程序。我在上传个人资料图片时卡住了。默认图片工作正常,但当我尝试上传新图片时,它就不起作用了。它没有显示任何错误。在我看来,存储文件可能有问题,但我不知道是什么问题。
当我尝试上传图片时,页面只是刷新,默认图片仍然存在。 profile_pictures 文件夹中也没有保存任何内容。这是我的布局:
/Users/korisnik/PycharmProjects/Blog
├── project
├── ├──core
| ├──posts
| ├──static
| | ├──profile_pictures
| | ├──default_profile_picture.jpg
| ├──templates
| ├──users
| | ├──__init__.py
| | ├──forms.py
| | ├──picture_handler.py
| | ├──views.py
| ├──__init__.py
| ├──models.py
├──app.py
这是我的代码:
picture_handler.py
def add_profile_picture(pic_upload, username):
filename = pic_upload.filename
extension_type = filename.split(".")[-1]
storage = str(username) + "." + extension_type
filepath = os.path.join(current_app.rooth_path, "static/profile_pictures", storage)
picture_size = (100, 100)
pic = Image.open(pic_upload)
pic.thumbnail(picture_size)
pic.save(filepath)
return storage
users.views.py
@users.route("/profile", methods=["GET", "POST"])
@login_required
def profile():
form = ProfileForm()
if form.validate_on_submit():
if form.picture.data:
username = current_user.username
pic = add_profile_picture(form.picture.data, username)
current_user.profile_image = pic
current_user.username = form.username.data
db.session.commit()
return redirect(url_for("users.profile"))
elif request.method == "GET":
form.username.data = current_user.username
profile_image = url_for("static", filename="profile_pictures/" + current_user.profile_image)
return render_template("profile.html", profile_image=profile_image, form=form)
profile.html
{% extends "base.html"%}
{% block content %}
<div class="jumbotron">
<div align="center">
<h1>Welcome {{current_user.username}}</h1>
<img src="{{url_for('static', filename='profile_pictures/' + current_user.profile_image)}}" align="center">
</div>
</div>
<div class="container">
<form method="POST" action="" enctype="multipart/form-data">
{{form.hidden_tag()}}
<div class="form-group">
{{form.username.label(class="form-group")}}{{form.username(class="form-control")}}<br>
</div>
<div class="form-group">
{{form.picture.label(class="form-group")}}{{form.picture(class="form-control")}}<br>
</div>
<div class="form-group">
{{form.submit(class="btn btn-primary")}}
</div>
</form>
</div>
{% endblock %}
答案在我的 init.py 文件中,我在其中将我的数据库目录定义为 basedir
basedir = os.path.abspath(os.path.dirname(__file__))
所以而不是 current_app.rooth_path
filepath = os.path.join(current_app.rooth_path, "static/profile_pictures", storage)
我只需要把 basedir
filepath = os.path.join(basedir, "static/profile_pictures", storage)
可能是拼写错误:
filepath = os.path.join(current_app.rooth_path, "static/profile_pictures", storage)
尝试使用 root 代替 rooth
filepath = os.path.join(current_app.root_path, "static/profile_pictures", storage)
我是新来的,也是初学者,所以我会很感激你 help.I 我正在尝试在 Flask 中做博客应用程序。我在上传个人资料图片时卡住了。默认图片工作正常,但当我尝试上传新图片时,它就不起作用了。它没有显示任何错误。在我看来,存储文件可能有问题,但我不知道是什么问题。 当我尝试上传图片时,页面只是刷新,默认图片仍然存在。 profile_pictures 文件夹中也没有保存任何内容。这是我的布局:
/Users/korisnik/PycharmProjects/Blog
├── project
├── ├──core
| ├──posts
| ├──static
| | ├──profile_pictures
| | ├──default_profile_picture.jpg
| ├──templates
| ├──users
| | ├──__init__.py
| | ├──forms.py
| | ├──picture_handler.py
| | ├──views.py
| ├──__init__.py
| ├──models.py
├──app.py
这是我的代码:
picture_handler.py
def add_profile_picture(pic_upload, username):
filename = pic_upload.filename
extension_type = filename.split(".")[-1]
storage = str(username) + "." + extension_type
filepath = os.path.join(current_app.rooth_path, "static/profile_pictures", storage)
picture_size = (100, 100)
pic = Image.open(pic_upload)
pic.thumbnail(picture_size)
pic.save(filepath)
return storage
users.views.py
@users.route("/profile", methods=["GET", "POST"])
@login_required
def profile():
form = ProfileForm()
if form.validate_on_submit():
if form.picture.data:
username = current_user.username
pic = add_profile_picture(form.picture.data, username)
current_user.profile_image = pic
current_user.username = form.username.data
db.session.commit()
return redirect(url_for("users.profile"))
elif request.method == "GET":
form.username.data = current_user.username
profile_image = url_for("static", filename="profile_pictures/" + current_user.profile_image)
return render_template("profile.html", profile_image=profile_image, form=form)
profile.html
{% extends "base.html"%}
{% block content %}
<div class="jumbotron">
<div align="center">
<h1>Welcome {{current_user.username}}</h1>
<img src="{{url_for('static', filename='profile_pictures/' + current_user.profile_image)}}" align="center">
</div>
</div>
<div class="container">
<form method="POST" action="" enctype="multipart/form-data">
{{form.hidden_tag()}}
<div class="form-group">
{{form.username.label(class="form-group")}}{{form.username(class="form-control")}}<br>
</div>
<div class="form-group">
{{form.picture.label(class="form-group")}}{{form.picture(class="form-control")}}<br>
</div>
<div class="form-group">
{{form.submit(class="btn btn-primary")}}
</div>
</form>
</div>
{% endblock %}
答案在我的 init.py 文件中,我在其中将我的数据库目录定义为 basedir
basedir = os.path.abspath(os.path.dirname(__file__))
所以而不是 current_app.rooth_path
filepath = os.path.join(current_app.rooth_path, "static/profile_pictures", storage)
我只需要把 basedir
filepath = os.path.join(basedir, "static/profile_pictures", storage)
可能是拼写错误:
filepath = os.path.join(current_app.rooth_path, "static/profile_pictures", storage)
尝试使用 root 代替 rooth
filepath = os.path.join(current_app.root_path, "static/profile_pictures", storage)