【问题标题】:profile picture won't upload in python/flask个人资料图片不会在 python/flask 中上传
【发布时间】:2021-02-01 13:17:10
【问题描述】:

我是新手和初学者,所以我会感谢你的帮助。我正在尝试在 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 %}

【问题讨论】:

  • 如果您执行 CTRL-F5 会显示新图像吗?想知道是不是缓存问题。您也可以尝试在加载时在图像末尾添加一个随机数,以查看是否清除它
  • 仍然无法正常工作.. 当我打印 profile_image 时,它​​总是给我 /static/profile_pictures/default_profile_picture.jpg
  • 我明白了!而不是 current_app.rooth_path 我需要按照我在 init.py 中定义的那样放置 basedir
  • 我可以建议您将其发布为您的答案,以便将来对其他人有所帮助吗?

标签: python flask jinja2


【解决方案1】:

答案在我的 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)

【讨论】:

    【解决方案2】:

    可能是拼写错误:

    filepath = os.path.join(current_app.rooth_path, "static/profile_pictures", storage)
    

    代替rooth,试试root

    filepath = os.path.join(current_app.root_path, "static/profile_pictures", storage)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-10
      • 2014-04-14
      • 2018-06-06
      • 2015-05-22
      • 1970-01-01
      • 2014-10-05
      • 2019-08-31
      • 2018-01-08
      相关资源
      最近更新 更多