Home>

There is a registration form. Please tell me how you can add a profile photo to the database and immediately get it back. I know that you can somehow useAJAXbut I don’t know how to implement it. When registering, only the name of the image is added to the database, and I cannot get it on the pageprofile... What could be the problem?

Registration Code:

from flask_mysqldb import MySQL
import MySQLdb.cursors
UPLOAD_FOLDER= 'static /uploads /'
@ app.route ('/register', methods= ['GET', 'POST'])
def register ():
    if request.method== 'POST':
        firstname= request.form ['firstname']
        lastname= request.form ['lastname']
        email= request.form ['email']
        filenames= request.files ['files']
    uploaded_files= request.files.getlist ("file []")
    filenames= []
    for file in uploaded_files:
        if file and allowed_file (file.filename):
            filename= secure_filename (file.filename)
            file.save (os.path.join (app.config ['UPLOAD_FOLDER'], filename))
            filenames.append (filename)
    # This line is essential, store the data in session
    session ['filenames']= filenames
        cur= mysql.connection.cursor ()
        session ['email']= email
        session ['firstname']= firstname
        cur.execute ('INSERT INTO users.data (firstname, lastname, email) VALUES (% s,% s,% s)', (firstname, lastname, email))
        mysql.connection.commit ()
        cur.close ()
        return redirect (url_for ('profile'))

profile.html:

@ app.route ('/profile', methods= ['GET'])
def profile ():
    if request.method== 'GET':
        return render_template ('profile.html', data= data, filenames= filenames)

register.html:

<
input type= "firstname" name= "firstname" placeholder= "Firstname" >
<
input type= "lastname" name= "lastname" placeholder= "Lastname" >
<
input type= "email" name= "email" placeholder= "First name" >
<
input type= 'file' name= "files" id= "uploadPDF" >
<
button >
Register <
/button >

profile.html:

<
h2 >
Welcome, {{firstname}} <
/h2 >
{% for file in filenames%}
  <
h1 >
{{file}} <
/h1 >
{% endfor%}