Good day
I’m trying to create a simple login page but I keep on getting an error: "Message: Unsupported method (‘POST’). Error code explanation: 501 - Server does not support this operation. No matter which project I try that has a POST statement it returns the error: 501 Unsupported method (‘POST’)`:
from flask import Flask, render_template, request, redirect, url_for, session
from flask_mysqldb import MySQL
import MySQLdb.cursors
import re
app = Flask(__name__)
app.secret_key = 'xyzsdfg'
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'user-system'
mysql = MySQL(app)
@app.route('/')
@app.route('/login', methods =['GET', 'POST'])
def login():
mesage = ''
if request.method == 'POST' and 'email' in request.form and 'password' in request.form:
email = request.form['email']
password = request.form['password']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM user WHERE email = % s AND password = % s', (email, password, ))
user = cursor.fetchone()
if user:
session['loggedin'] = True
session['userid'] = user['userid']
session['name'] = user['name']
session['email'] = user['email']
mesage = 'Logged in successfully !'
return render_template('user.html', mesage = mesage)
else:
mesage = 'Please enter correct email / password !'
return render_template('login.html', mesage = mesage)
@app.route('/logout')
def logout():
session.pop('loggedin', None)
session.pop('userid', None)
session.pop('email', None)
return redirect(url_for('login'))
@app.route('/register', methods =['GET', 'POST'])
def register():
mesage = ''
if request.method == 'POST' and 'name' in request.form and 'password' in request.form and 'email' in request.form :
userName = request.form['name']
password = request.form['password']
email = request.form['email']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM user WHERE email = % s', (email, ))
account = cursor.fetchone()
if account:
mesage = 'Account already exists !'
elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
mesage = 'Invalid email address !'
elif not userName or not password or not email:
mesage = 'Please fill out the form !'
else:
cursor.execute('INSERT INTO user VALUES (NULL, % s, % s, % s)', (userName, email, password, ))
mysql.connection.commit()
mesage = 'You have successfully registered !'
elif request.method == 'POST':
mesage = 'Please fill out the form !'
return render_template('register.html', mesage = mesage)
if __name__ == "__main__":
app.run()
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Enter your email to get $200 in credit for your first 60 days with DigitalOcean.
New accounts only. By submitting your email you agree to our Privacy Policy.
Hi there,
Are you really sending a POST request? Browsers default to GET when you pop a URL into them. Try using Postman, curl, or even an HTML form to send a POST and see if you are still getting that error.
The 501 error you are encountering is a HTTP status code that means “Not Implemented.” This typically means that the server does not recognize the method, or it lacks the ability to fulfill the request.
Looking at your code, it seems fine, and it should work assuming you’re using the correct HTTP methods in your requests and your MySQL server is correctly configured.
Here are a few more things you could check:
Form Data: Double-check that you are passing the correct data with your POST request. The keys in your request should match those expected by your Flask application (‘email’, ‘password’ and ‘name’ for the
/register
endpoint, ‘email’ and ‘password’ for the/login
endpoint).HTML Forms: If you’re using HTML forms to submit the data, ensure that the form is properly formatted, the
method="post"
attribute is included in the form tag, and thename
attribute of the input fields match the keys that your Flask app is expecting.Server Logs: Flask normally logs useful debug information, so make sure to check your server logs. You might find more details about the error there.
Try running Flask in debug mode (
app.run(debug=True)
) for more detailed error messages, eg:Let me know how it goes!
Best,
Bobby