Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Not Allowed flask error 405

I am developing a flask registration form, and I receive an error:

error 405 method not found.

Code:

import os
# Flask
from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash, Markup, send_from_directory, escape
from werkzeug import secure_filename

from cultura import app

# My app
from include import User

@app.route('/')
def index():
    return render_template('hello.html')

@app.route('/registrazione', methods=['POST']) 
def registration():
    if request.method == 'POST':
        username= request.form.username.data
        return render_template('registration.html', username=username)
    else :
        return render_template('registration.html')

registration.html:

<html>
<head> <title>Form di registrazione </title>
 </head> 

    <body>
    {{ username }}
    <form id='registration' action='/registrazione' method='post'>
    <fieldset >
    <legend>Registrazione utente</legend>
    <input type='hidden' name='submitted' id='submitted' value='1'/>
    <label for='name' >Nome: </label> 
    <input type='text' name='name' id='name' maxlength="50" /> <br>
    <label for='email' >Indirizzo mail:</label>
    <input type='text' name='email' id='email' maxlength="50" />
     <br>
    <label for='username' >UserName*:</label>
    <input type='text' name='username' id='username' maxlength="50" />
     <br>
    <label for='password' >Password*:</label>
    <input type='password' name='password' id='password' maxlength="50" />
    <br>
    <input type='submit' name='Submit' value='Submit' />

    </fieldset>
    </form>
    </body>
    </html>

when I visit localhost:5000/registrazione, I receive the error. What am I doing wrong?

like image 985
Matteo Avatar asked Feb 10 '14 22:02

Matteo


People also ask

How do I fix 405 Method not allowed in flask?

To fix POST Error 405 Method Not Allowed with Flask Python, we should make sure the action attribute of the form is set to the URL of the view that accepts POST requests. to create the template view. to add a form that has the action attribute set to the URL for the template view that we get with url_for('template') .

What causes 405 Method not allowed?

The 405 Method Not Allowed error is an HTTP response status that tells you that a web browser has made a request to access one of your website's pages. Your web server has received the request and recognized it but has rejected the specific HTTP method that was used.


3 Answers

This is because you only allow POST requests when defining your route.

When you visit /registrazione in your browser, it will do a GET request first. Only once you submit the form your browser will do a POST. So for a self-submitting form like yours, you need to handle both.

Using

@app.route('/registrazione', methods=['GET', 'POST']) 

should work.

like image 132
Lukas Graf Avatar answered Nov 07 '22 09:11

Lukas Graf


Just for people reading on it now. You have to render the /registrazione first, befor you can access the form data. Just write.

@app.route("/registrazione")
    def render_registrazione() -> "html":
        return render_template("registrazione.html")

before you define def registration(). Sequence is key. You can't access data before the even are available. This is my understanding of the problem.

like image 23
nullbyte Avatar answered Nov 07 '22 09:11

nullbyte


change name of method registration

@app.route('/registrazione', methods=['POST']) 
def registrazione(): 
    if request.method == 'POST':
       username= request.form.username.data
       return render_template('registration.html', username=username)
   else :
       return render_template('registration.html')
like image 32
RADHE Avatar answered Nov 07 '22 08:11

RADHE