Building a web application can be a challenging task, but using Flask can simplify and streamline the process. Flask is a micro web framework written in Python, and it allows developers to create web applications quickly and easily.
One crucial aspect of building a web application is implementing a login system. In this article, we’ll explore the steps involved in setting up a login page and route in Flask.
Setting up Login Page and Route
1) Defining the Login Route
Before we start building our login page, we need to define a URL route for it. Flask allows us to define routes easily using decorators.
In this case, we will define a route for the login page. The following code shows how to define the login route:
from flask import Flask
app = Flask(__name__)
@app.route('/login')
def login():
return 'Login Page'
The @app.route
decorator creates a URL route for the login page. Here, we define the login URL as ‘/login’.
The login()
function is called when the ‘/login’ URL is accessed. In this example, the function simply returns the string ‘Login Page’.
2) Handling Login Page Logic
After defining the URL route for our login page, we need to implement the logic behind the login page. The user will enter their login credentials on the login page, which will be validated on the server-side, and the user will be redirected to the dashboard page if their credentials are valid.
We can handle the login page logic using HTTP methods, POST and GET, which are used to send data and receive data from the server.
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST': # Check if HTTP method is POST
username = request.form['username'] # Get username from POST request
password = request.form['password'] # Get password from POST request
# Validate the login credentials
if username == 'admin' and password == 'admin':
return redirect('/dashboard') # Redirect to dashboard page if credentials are valid
else:
return 'Invalid username or password. Please try again.' # Return error message if credentials are invalid
else:
return render_template('login.html') # Render the login template if HTTP method is not POST
In the above code snippet, we have defined the login()
function to handle GET and POST requests.
If the HTTP method is POST, the function retrieves the entered username and password values from the POST request. The function then checks if the entered credentials are valid.
If the credentials are valid, the function redirects the user to the dashboard page. If the credentials are invalid, the function returns an error message.
If the HTTP method is not POST, the login()
function simply renders the login template. For that, we’ll need to create a login page template.
3) Adding a Login Template
Creating the Login Template
To create the login template, we need to create a new HTML file named ‘login.html’ in the ‘templates’ directory. Flask automatically looks for templates in this directory.
We’ll use the Bootstrap framework to style our login page. Here’s an example HTML code for a simple login template: