I am using flask in pycharm to create a simple website. I am working on features that allow the user to register an account for the website and login using their created credentials. My problem currently is that the registration form isn’t consistently redirecting to the login page. It is only refreshing the page after providing a username and suitable password.
Heres what i’ve currently tried.
Registration HTML Form:
<form method="POST" action="{{url_for('register')}}">
<!-- Defines username Fields -->
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<!-- Defines password Fields -->
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<!-- Defines Register Button -->
<button type="submit">Register</button>
</form>
Login HTML Form:
<form method="POST" action="{{url_for('login')}}">
<!-- Defines username Fields -->
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<!-- Defines password Fields -->
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<!-- Defines Login Button -->
<button type="submit">Login</button>
</form>
Web Page Routes for both login and registration blocks:
from datetime import date
import re
from flask import Flask, render_template, request, redirect, url_for, session
# Calls the imported flask function and sets to render the web pages in the "template" form
app = Flask(__name__, template_folder='template')
app.secret_key = 'your_secret_key'
#User Data and variables being used
users = [{'username': 'password'}]
#Password Complexity
PASSWORD_REGEX = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[@$!%*?&])[A-Za-zd@$!%*?&]{12,}$'
# Defining the website routes
@app.route('/')
# Sets up registration page for the site
@app.route('/register', methods=['GET', 'POST'])
def register():
'''Function that renders the registration page'''
if request.method == 'POST':
# Establishes the username and password variables to be inputted into the form
username = request.form['username']
password = request.form['password']
if username in users:
# Refreshes the page and prints a message when username already exists.
return render_template('register.html', message='Username already exists.')
if not re.match(PASSWORD_REGEX, password):
# Refreshes the page and prints a message when password isn't complex enough
return render_template('register.html', message='Password complexity not met.')
users[username] = password
# Redirects to the login screen
return redirect(url_for('login'))
return render_template('register.html',date=date.today())
# Sets up login page for the site
@app.route('/login', methods=['GET', 'POST'])
def login():
'''Function that renders the login page'''
if request.method == 'POST':
# Establishes the username and password variables to be inputted into the form
username = request.form['username']
password = request.form['password']
if username in users and users[username] == password:
session['username'] = username
# Redirects to the home page screen
return redirect(url_for('home_page'))
# Refreshes the page and prints a message when username already exists.
return render_template('login.html', message='Invalid credentials.')
#Renders the login page
return render_template('login.html',date=date.today())
New contributor
robert crawford is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.