Friday, 19 February 2021

How to solve 405 Method Not Allowed (flask)

I'm keep getting the 405 error, I've changed my code following the other related posts but still not working.

routes.py

from flask import render_template, redirect, flash, url_for, abort, request
from flask_login import current_user, login_user, logout_user, login_required
from flask import Flask
import views.model as mod
app = Flask(__name__)


@app.route('/')
def index():
    mod.check_database_init()
    return render_template("index.html")

@app.route('/login',methods = ['GET','POST'])
def login():
    
    if(request.method == 'POST'):
        print("post")
        name = request.form['username']
        password = request.form['password']
        print(name, password)
        return redirect(url_for("index"))
    else:
        return render_template("login.html")


@app.route('/register', methods = ['GET','POST'])
def register():
    return render_template("register.html")

login.html


Liquid error: This liquid context does not allow includes.


<title>Login</title>

<form action="/login" method="POST">
    <label>Username</label>
    <input type = "text" placeholder = "username" name="username" required>
    <label>Password</label>
    <input type = "password" placeholder="password" name = "password" required>
    <input type="submit">

    <p>
        Don't have an acoount? <a href="/register">Click me</a> to create a new account
    </p>
</form>

<a href="/">Back to homepage </a>

I tried this method, https://stackoverflow.com/a/62464826/13239458, it works but I don't know what went wrong in the code above.

server.py

from flask import Flask
from flask_login import LoginManager
import views.routes as rou
import views.database as db


app = Flask(__name__)


app.add_url_rule('/', view_func=rou.index)
app.add_url_rule('/login', view_func=rou.login)
app.add_url_rule('/register', view_func=rou.register)


if __name__ == "__main__":
    app.run(host='127.0.0.1', debug = True)

UPDATE

I just change method type into GET (in login.html) and there will be no error and I can see the input from the console, so I think the login method cannot accept the POST method but I have no idea why is that.



from How to solve 405 Method Not Allowed (flask)

No comments:

Post a Comment