Method Not Allowed The method is not allowed for the requested URL. 405 Error

Issue

I am new to building API. I am building a very Simple API: When executed, The HTML page displaying API will ask to enter name: the Api will just return: "Hello {name}". I am getting 405 Error. Here is my code for App.py and HTML page:

from app import app
from flask import render_template
from flask import request

@app.route('/')
def home():
   return "hello no  world!"

@app.route('/template',methods=['POST','GET'])
def template():
    output = [request.form.values()]
    output = output[0]
    return render_template('home.html',prediction_text='Hello {}'.format(output))

and my HTML code for home.html:

!doctype html>

<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Welcome home</title>
  </head>

  <body>
    <div class="login">
        <h1>Enter your name</h1>

     <!-- Main Input For Receiving Query-->
    <form action="{{ url_for('template')}}"method="post">
        <input type="text" name="name" placeholder="Name" required="required" />

        <button type="submit" class="btn btn-primary btn-block btn-large">Enter</button>
    </form>

   <br>
   <br>
   {{ prediction_text }}

 </div>
  </body>
</html>

I have looked at several other StackOverflow forums with just this issue. It seems like there is something wrong with "GET" or "POST" method but I can not seem to figure out what? Maybe one of you could see something I did not. I am running this API Inside a Docker so "app = Flask(name)" are stored elsewhere , If that is relevant.

Method Not Allowed – The method is not allowed for the requested URL

Flask – POST – The method is not allowed for the requested URL

https://www.reddit.com/r/flask/comments/a04aew/ask_flask_help_me_to_find_the_error_in_my_code/

Solution

This question is now resolved. I changed two things:

From HTML:
Changing:

<form action="{{ url_for('template')}}"method="post">

to

 <form action="{{ url_for('template')}}"method="get">

And Changing from Flask API:

output = [request.form.values()]
output = output[0]
return render_template('home.html',prediction_text='Hello {}'.format(output))

to

output = request.args.values()
return render_template('home.html',prediction_text="Hello {}?".format(list(output)[:]))

Answered By – Faraz Ali Khan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published