Python Flask is a powerful web framework that allows developers to create dynamic and interactive web applications. One of the key features of Flask is its ability to handle user authentication and authorization. In this article, I will guide you through the process of creating a login page using Flask.
Setting Up Flask
Before we dive into creating the login page, we need to make sure Flask is installed on our system. In order to install Flask, you can use the following command:
pip install flask
Once Flask is installed, we can start building our login page.
Creating the Login Page
To create the login page, we need to define a route in our Flask application. A route is a URL pattern that corresponds to a specific function in our code. In our case, we want the login page to be accessible at the ‘/login’ URL.
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if username == 'admin' and password == 'password':
return redirect('/dashboard')
else:
return render_template('login.html', error='Invalid username or password')
else:
return render_template('login.html')
In the code above, we first import the necessary modules from Flask. Then, we create an instance of the Flask class and define a route for the login page. Inside the login function, we handle both GET and POST requests. If the request is a POST request, we retrieve the username and password from the form data. We then check if the username and password match our predefined admin credentials. If they do, we redirect the user to the dashboard. If the credentials do not match, we pass an error message to the login template to display an error message to the user. If the request is a GET request, we simply render the login template.
Creating the Login Template
Next, we need to create the login template. The login template is an HTML file that defines the structure and layout of the login page. Here’s an example of a basic login template:
<!-- login.html -->
<h2>Login</h2>
<form action="/login" method="post">
<input type="text" name="username" placeholder="Username"><br>
<input type="password" name="password" placeholder="Password"><br>
<input type="submit" value="Login">
</form>
<p style="color: red;">{{ error }}</p>
In the login template, we use HTML tags to define the structure of the login page. We use the form tag to create a login form with input fields for the username and password. The action attribute of the form tag is set to “/login” to specify that the form should be submitted to the “/login” URL. The method attribute is set to “post” to specify that the form data should be sent using the POST method.
Conclusion
Creating a login page using Python Flask is a straightforward process. With Flask’s routing capabilities and the ability to handle form submissions, we can easily implement a secure and user-friendly login system. By following the steps outlined in this article, you should now have a solid understanding of how to create a login page using Flask.
For more information and detailed documentation on Flask, I recommend visiting the official Flask website at https://flask.palletsprojects.com/.