0

I am attempting to create a basic login page on a raspberry pi, but when I try to login to the page I keep getting a 405 error. I have researched the issue but it seems to only appear if you aren't using the correct methods, but since it's a login screen POST and GET should be more than sufficient.

Python

from flask import Flask
from flask import Flask, flash, redirect, render_template, request, session, abort
import os

app = Flask(__name__)

@app.route('/')
def home():
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        return "Hello Boss!"

@app.route('/login', methods=['POST', 'GET'])
def do_admin_login():
    if request.form['psw'] == 'password' and request.form['id'] == 'admin':
        print ("Hey ya!")
    else:
        print('wrong password!')
    return home()

if __name__ == "__main__":
    app.secret_key = os.urandom(12)
    app.run(debug=True,host='0.0.0.0', port=80)

HTML

<!DOCTYPE html>
<html>
<body>
<h1>Please login</h1
<form method="POST">
    <input name="id" type="text" placeholder="Enter UserID">
    <br>
    <input name="psw" type="password" placeholder="Enter Password">
    <input type="submit">
</form>
</body>
</html>

I have been redirected to a different question which resolves it through changing the action part of the however mine does not have action so this should not be the same issue. I have attempted adding an action element to it but that has not changed anything

Blue Dabba Dee
  • 113
  • 1
  • 4

1 Answers1

1

The problem is that you need to specify the action attribute in HTML:

<form method="POST" action="{{url_for('do_admin_login')}}">
    <input name="id" type="text" placeholder="Enter UserID">
    <br>
    <input name="psw" type="password" placeholder="Enter Password">
    <input type="submit">
</form>

As you may notice that I used url_for function to generate a URL to the given endpoint with the method provided ( do_admin_login() in this case ). So it is worth to mention that you need to change your statement return home() to return url_for("home").

rockikz
  • 586
  • 1
  • 6
  • 17