Flask upload and display images from MYSQL

  flask, flask-mysql, image, mysql, python

Im working on a website by using Flask. In database my image type blob and Im using flask-mysql library. I want to upload image to the database and display from there but when I trying to display, my output is None in ‘goster.html’

Here is my UploadForm:

class UploadForm(Form):
   billcontent = StringField("Bill Content")
   bill = FileField("File")

and here ‘/deneme’:

@app.route("/deneme",methods = ["POST","GET"])
@login_required
def deneme():
   form = UploadForm(request.form)
   if request.method == "POST":
       billcontent = form.billcontent.data
       bill = form.bill.data
       company = get_current_user_company()
       cursor = mysql.connection.cursor()
       sorgu = "Insert into deneme(billcontent,bill,company) VALUES(%s,%s,%s)"
       cursor.execute(sorgu,(billcontent,bill,company,))
       mysql.connection.commit()
       cursor.close()
       flash("{} faturası başarıyla eklendi!".format(billcontent))
       return redirect(url_for("index"))
   return render_template("deneme.html",form=form)

and here ‘/goster’:

@app.route("/goster")
@login_required
def goster():
   cursor = mysql.connection.cursor()
   sorgu = "Select * From deneme "
   result = cursor.execute(sorgu)
   if result>0:
       image = cursor.fetchone()
       return render_template("goster.html",image=image)
   else:
       return render_template("goster.html")

deneme.html:

{% extends "layout.html" %}


{% block middleside %}
{% from "includes/formhelpers.html" import render_field %}
<form method = "POST" enctype="multipart/form-data">

{{ render_field(form.billcontent,class = "form-control") }}
{{ render_field(form.bill,class = "form-control") }}
<button type="submit">Upload Bill</button>

</form>
{% endblock middleside %}

goster.html:

{% extends "layout.html" %}

{% block middleside %}

{{image.bill}}

{% endblock middleside %}

Source: Python Questions

LEAVE A COMMENT