How To Create Login Page In Python Using Flask

Creating a login page is an essential component of many web applications. It allows users to securely access their accounts and protects sensitive information. In this article, I will guide you through the process of creating a login page in Python using the Flask web framework. Flask is a popular choice for developing web applications due to its simplicity and flexibility.

To begin with, make sure you have Flask installed on your system. If not, you can install it by executing the following command in your command line interface:


pip install flask

Once Flask is installed, create a new Python file and import the necessary modules:


from flask import Flask, render_template, request, redirect

Next, initialize the Flask application:


app = Flask(__name__)

Now, let’s create the login page route. A route is a URL that the user can visit in order to interact with your web application. In this case, we will create a route for the login page where users can enter their credentials.


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

# Check if the credentials are valid (you can implement your own validation logic here)
if username == 'admin' and password == 'password':
# Redirect the user to the home page after successful login
return redirect('/home')
else:
# Display an error message if the credentials are invalid
return render_template('login.html', error='Invalid credentials')

# Render the login page template for GET requests
return render_template('login.html')

In the code above, we have defined a route for ‘/login’ that supports both GET and POST requests. When a user submits the login form (POST request), we retrieve the entered username and password from the request form data. We then validate the credentials and redirect the user to the home page if they are valid. If the credentials are invalid, we render the login page again with an error message. For GET requests, we simply render the login page template.

Now, let’s create the login page template. Create a new HTML file named ‘login.html’ in a templates folder:





Login Page

Login

{% if error %}

{{ error }}

{% endif %}






In the login page template, we have a simple HTML form that accepts the username and password. The form action is set to ‘/login’ and the method is set to ‘POST’, which corresponds to the route we defined earlier. If there is an error message, it is rendered in red. Remember to include the necessary CSS styling to make the page visually appealing.

That’s it! You have successfully created a login page in Python using Flask. You can now run your application and visit the login page by entering the URL: http://localhost:5000/login. Feel free to add additional features such as password hashing and database integration to enhance the security and functionality of your login system.

Conclusion

In this article, we explored how to create a login page in Python using the Flask web framework. We learned how to define a route, process form data, and render templates to create a functional login system. Remember to always prioritize security when developing login pages and consider implementing additional measures such as password hashing and CSRF protection. Happy coding!