-1

I am trying to make a form that uploads data and a pic on the SQL database the HTML form is working fine but when I press submit it say the page isn't working and doesn't insert any data on the table can u plz help and check what is the problem with my code

Here is my python code #main.py

from flask import Flask, flash, request, redirect, url_for, render_template
import urllib.request
import os
from werkzeug.utils import secure_filename
from Dbaconnection import dbaconnect
     
app = Flask(__name__)
     
app.secret_key = "cairocoders-ednalan"

UPLOAD_FOLDER = 'static/uploads/'
  
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
  
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
  
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
      
  
@app.route('/')
def home():
    return render_template('index.html')
@app.route('/webproject', methods=['GET','POST'])
def upload_image():
 
    if 'file' not in request.files:
        flash('No file part')
        return redirect(request.url)
    file = request.files['file']
    if file.filename == '':
        flash('No image selected for uploading')
        return redirect(request.url)
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        data=request.form
        db = dbaconnect();
        dbcursor = db.cursor()
        print(data['file'])
        dbcursor.execute("INSERT INTO webproject(name,Address,Photo) VALUES(%s,%s,%s)",(data['Name'],data['Address'],data['Photo'],))
        db.commit()
        flash('Image successfully uploaded and displayed below')
        return render_template('index.html', filename=filename)
    else:
        flash('Allowed image types are - png, jpg, jpeg, gif')
        return redirect(request.url)
    
  
@app.route('/display/<filename>')
def display_image(filename):
    return redirect(url_for('static', filename='uploads/' + filename), code=301)
  
if __name__ == "__main__":
    app.run(debug=True)

and here the HTML form code #index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Database upload form</title>

    <!-- Font Icon -->

    <link rel="stylesheet" href=" {{url_for('static',filename='fonts/material-icon/css/material-design-iconic-font.min.css')}}">

    <!-- Main css -->
    <link rel="stylesheet" href="{{url_for('static',filename='css/style.css')}}">
</head>

<body>

    <div class="main">

        <div class="container">
            <div class="signup-content">
                <img src="{{ url_for('display_image', filename=filename) }}">
                <form method="GET","POST" id="signup-form" class="signup-form" action = '/webproject' enctype="multipart/form-data"/>
                    <h2>Assignment</h2>
                    <p class="desc">Enter The Student Data To store</span></p>
                    <div class="form-group">
                        <input type="text" class="form-input" name="Name" id="name" placeholder="Your Name" />
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-input" name="Address" id="address" placeholder="Your Address" />
                    </div>
                <div class="form-group">
                   <div class="costom-file">
                    <label class="custom-file-lable" for="image">file input</label>
                    <input type="file" class="custom-file-input" id="Photo" name="Photo" VALUE="upload a file" />
                  </div>
                    <div class="form-group">
                        <input type="submit" name="upload on db" id="submit" class="form-submit submit" value="Submit" />
                    </div>
                </form>
            </div>
        </div>

    </div>

    <!-- JS -->
    <script href="{{url_for('static',filename='vendor/jquery/jquery.min.js')}}"></script>
    <script href="{{url_for('static',filename='js/main.js')}}"></script>
</body>
<!-- This templates was made by Colorlib (https://colorlib.com) -->

</html>

1 Answers1

0
  1. You can't put multiples method in html form tag. you can only choose one either get or post
  2. Your backend code for getting file input from html form is invalid

in your html form, you said:

<input type="file" class="custom-file-input" id="Photo" name="Photo" VALUE="upload a file" />

but in your backend code:

request.files['file'] -> request.files['Photo']

as mentioned in this article

Dharman
  • 30,962
  • 25
  • 85
  • 135
Arief Kahfi
  • 25
  • 1
  • 5