DEV Community

Thasethi
Thasethi

Posted on

code 501 message unsupported method ('post') python

Good day

I am trying to make a simple login page but when I run it I get and error code "code 501 message unsupported method ('post') python" I'm using python -m http.server 3000 for my server and I have a data base of user's in MySQL workbench. May you please assist.
from flask import Flask, render_template, request, session, redirect
from flask_mysqldb import MySQL
from http.server import HTTPServer, BaseHTTPRequestHandler

app = Flask(name)
app.secret_key = '1234' # Set a secret key for session management

MySQL configuration

app.config['MYSQL_DATABASE_HOST'] = 'localhost:3306'
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'Core@123!'
app.config['MYSQL_DATABASE_DB'] = 'login'
mysql = MySQL(app)

@app.route('/')
def home():
return 'Welcome to the login system'

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']

    cursor = mysql.connection.cursor()
    cursor.execute("SELECT * FROM users WHERE username = %s AND password = %s", (username, password))
    user = cursor.fetchone()

    if user:
        session['username'] = username
        return redirect('/dashboard')
    else:
        return 'Invalid username or password'

return render_template('login.html')
Enter fullscreen mode Exit fullscreen mode

@app.route('/dashboard')
def dashboard():
if 'username' in session:
return 'Welcome to the dashboard, ' + session['username']
else:
return redirect('/login')

Top comments (0)