'python Unsupported method ('POST')

python Unsupported method ('POST') this program is meant to convert images to text but I kept getting this error over and over I don't know what is the problem I don't know exactly if the problem with the HTML code or this code or am I missing other files I don't quite understand the HTML code so I will be thankful if you elaborate


    from gettext import gettext
    from click import get_text_stream
    from flask import Flask, render_template, request
    import os, pytesseract
    from jinja2 import Template 
    from flask_uploads import UploadSet, configure_uploads, IMAGES 
    from PIL import Image 
    
    
    project_dir = os.path.dirname(os.path.abspath(__file__))
    
    app = Flask(__name__, static_url_path='', static_folder = 'static', template_folder = 'gg.html')
    
    
    photos = UploadSet('photos', IMAGES)
    
    app.config['DEBUG'] = True 
    app.config['UPLOAD_FOLDER'] = 'images'
    
    
    
    class UploadText(object): 
         def __init__(self, file):
            self_file = pytesseract.image_to_string(Image.open(project_dir + '/imges/' + file))
    
    
    @app.route('/gg', methods =["POST" , "GET"])
    def home():
        if request.method == "POST":
            if 'photo' not in request.files:
                return 'there is no photo in form'
            name = request.form['img-name'] + '.jpg'
            photo = request.files['photo']
            path = os.path.join(app.config['UPLOAD_FOLDER'], name)
            photo.save(path)
            textObject = get_text_stream(name)
            
            return textObject.file
     
    
        return render_template('gg.html')
    if __name__ == ' __main__':
        app.run()

The HTML:

    <head>
    <meta charset = "utf-8">
    <title> Image to Text</title>
    
    </head>
    <body>
    
     <div class='text-center'><br><br> 
    
    <form method='post' enctype="multipart/form-data">
    
    <input type="file" class='btn btn-dark' name='photo'>
    <input id ='input' type='text' class ='form-control' placeholder='enter image name' name='img-name'>
    <input class = 'btn btn-dark' type='submit'>
     
    </form>
    
    </div>

    </body>
    <style> 
    
    #input{
        margin: auto;
        width: auto;
    }
    
    
    </style>



Solution 1:[1]

You don't specify an action attribute in the HTML <form> tag, which tells the form where to submit to. Instead try:

<form method='post' action='/gg' enctype="multipart/form-data">

Of course it's also possible to render the /gg part dynamically, based on the name of your python function home:

<form method='post' action='{{ url_for("home") }}' enctype="multipart/form-data">

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 v25