How To Create A Login Page In Python Flask

In this article, I will guide you through the process of creating a login page using Python Flask. Flask is a lightweight web framework that allows you to quickly build web applications using Python. Creating a login page is an essential feature for many web applications, as it provides a way for users to authenticate and access restricted content.

Getting Started

First, make sure you have Python installed on your machine. You can download the latest version of Python from the official website. Once you have Python installed, open your terminal or command prompt and install Flask by running the following command:

pip install flask

This will install Flask along with its dependencies.

Setting Up the Flask App

Next, let’s create a new directory for our Flask app. Open your terminal or command prompt and navigate to the desired location where you want to create your app. Then, run the following command to create a new directory:

mkdir my-login-app

Once the directory is created, navigate into it:

cd my-login-app

Inside the my-login-app directory, create a new file called app.py using your preferred text editor. This file will contain the code for our Flask app.

Now, let’s start by importing the necessary modules:

from flask import Flask, render_template, request, redirect, url_for

The Flask module is the core of our app, while the render_template function allows us to render HTML templates, the request module allows us to access incoming requests, and the redirect and url_for modules allow us to redirect the user to different URL routes.

Creating the Login Form

To create our login page, we need to design a form that will collect the user’s credentials. In Flask, we can use HTML templates to define the structure of our web pages. Inside the my-login-app directory, create a new directory called templates. Inside the templates directory, create a new file called login.html using your preferred text editor.

Here’s an example of the HTML code for our login form:

<form method="POST" action="{{ url_for('login') }}">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>

<label for="password">Password:</label>
<input type="password" id="password" name="password" required>

<input type="submit" value="Log In">
</form>

In the above code, we define a form with the method attribute set to “POST” and the action attribute set to {{ url_for('login') }}. The url_for() function is a Flask method that generates the URL for a given view function. In this case, it will generate the URL for the login function we will define later.

Inside the form, we have two input fields: one for the username and one for the password. Both fields have the required attribute set to ensure that the user must enter a value.

Handling the Login Request

Now that we have our form, let’s handle the login request. Inside the app.py file, define a new route called /login that will handle the POST request for the login form:

@app.route('/login', methods=['POST'])
def login():
# Get the values from the form
username = request.form['username']
password = request.form['password']

# Add your authentication logic here
if username == 'myuser' and password == 'mypassword':
return 'Logged in successfully!'
else:
return 'Invalid username or password'

In the above code, we first retrieve the values entered in the form using the request.form dictionary. Then, you can add your own authentication logic to validate the username and password. In this example, I have kept it simple by checking if the username is ‘myuser’ and the password is ‘mypassword’.

If the username and password are correct, we return the message ‘Logged in successfully!’. Otherwise, we return the message ‘Invalid username or password’.

Running the Flask App

To run our Flask app, go back to your terminal or command prompt and navigate to the my-login-app directory. Then, execute the following command:

python app.py

This will start the Flask development server, and you should see an output like this:

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Now, open your web browser and visit http://127.0.0.1:5000/login. You should see the login form we created earlier.

Enter the username and password as ‘myuser’ and ‘mypassword’, respectively. If you entered the correct credentials, you should see the message ‘Logged in successfully!’. Otherwise, you will see the message ‘Invalid username or password’.

Conclusion

In this article, we have learned how to create a login page in Python Flask. We covered the process of setting up a Flask app, designing the login form using HTML templates, handling the login request, and running the app using the Flask development server. Remember, this is just a basic example to get you started. In a real-world application, you would likely use a database to store user credentials and implement more robust authentication mechanisms.

I hope you found this article helpful in understanding how to create a login page using Python Flask. Happy coding!