Skip to content Skip to sidebar Skip to footer

Flask 405 Method Not Allowed

after click subimit button I have problem like in title, the problem start showing when I separated routes from app to views, when I go to localhost:5000/register/ all is good but

Solution 1:

Flask is not finding the POST method handler for the /register/ endpoint. You're mixing add_url_rule and @route. You just need the former if you want pluggable views.

I would recommend using the MethodView approach here. Have a separate class for each template and define the get() and post() methods within that.

app.py:

from flask import Flask
from views import Register, Login, Index

app = Flask(__name__)

app.add_url_rule('/', view_func=Index.as_view("index"))
app.add_url_rule('/login/', view_func=Login.as_view("login"))
app.add_url_rule('/register/', view_func=Register.as_view("register"))

if __name__ == '__main__':
    app.run(debug=True)

views.py:

from flask import Flask,render_template,request,redirect,abort
from flask.views import MethodView


classRegister(MethodView):
    defget(self):
        return render_template('register.html')

    defpost(self):
        req = request.form
        email = req.get("email")
        password = req["password"]
        phonenumber = request.form["phonenumber"]
        if email == ""or password == ""or phonenumber == "":
            feedback = "Please fill the form"
            alert = "fail"else:
            feedback = "Account created!"
            alert = "good"return render_template('register.html', feedback=feedback, alert=alert)


classIndex(MethodView):
    defget(self):
        print("CIAO")
        return render_template('index.html')


classLogin(MethodView):
    defget(self):
        return render_template('login.html')

Post a Comment for "Flask 405 Method Not Allowed"