How to save a image file on a Postgres database?

前端 未结 5 1183
后悔当初
后悔当初 2021-01-31 06:21

For learning purposes, I\'m creating a site using Python+Flask. I want to recover an image from database and show it on screen. But one step at a time.

I have no idea ho

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 06:45

    that's my solution, it can work in my website:

    @main.route('/upload', methods=['GET', 'POST'])
    def upload_avatar():
        if request.method == 'POST':
            file = request.files['file']
            if file and allowed_file(file.filename):
                current_user.avatar_local = file.read()
                db.session.add(current_user)
                db.session.commit()
                return redirect(url_for('main.user_page', username=current_user.username))
        return render_template('upload_avatar.html', user=current_user)
    

    using Flask,Flask-Alchemy to handle database.

    {% block edit_avatar  %}
        

    {% endblock %}

    that's html file.you can embed it in you html.

提交回复
热议问题