In Flask, routes are used to map URLs to Python functions. Each route corresponds to a specific page in the multi - page app. For example, if you have a home page, about page, and contact page, you will define separate routes for each of them.
from flask import Flask
app = Flask(__name__)
# Route for the home page
@app.route('/')
def home():
return "Welcome to the home page!"
# Route for the about page
@app.route('/about')
def about():
return "This is the about page."
# Route for the contact page
@app.route('/contact')
def contact():
return "Contact us at [email protected]"
if __name__ == '__main__':
app.run(debug=True)
For more complex multi - page apps, it is recommended to use templates. Templates allow you to separate the HTML structure from the Python code. Flask uses Jinja2 as its templating engine.
from flask import Flask, render_template
app = Flask(__name__)
# Route for the home page
@app.route('/')
def home():
return render_template('home.html')
# Route for the about page
@app.route('/about')
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=True)
In the above example, home.html
and about.html
are HTML files located in the templates
directory.
Static files such as CSS, JavaScript, and images are essential for styling and adding interactivity to your multi - page app. Flask provides a way to serve static files from a static
directory.
<!-- In templates/home.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>Home Page</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>Welcome to the home page!</h1>
</body>
</html>
Corporate websites often have multiple pages such as a home page, about page, services page, and contact page. Flask can be used to build these websites efficiently, allowing for easy content management and updates.
A blog platform usually consists of a main page showing a list of blog posts, individual post pages, an about page, and a contact page. Flask can handle the routing and rendering of these different pages.
E - commerce platforms need multiple pages for product listings, product details, shopping cart, and checkout. Flask can be used to build the front - end of these platforms, integrating with databases to manage product information.
Create a new directory for your project and navigate to it in the terminal. Then, create a virtual environment and activate it.
mkdir flask_multi_page_app
cd flask_multi_page_app
python -m venv venv
# On Windows
venv\Scripts\activate
# On Linux/Mac
source venv/bin/activate
Install Flask using pip
.
pip install flask
Create a Python file, for example, app.py
, and add the following code:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=True)
Create a templates
directory in your project and add home.html
and about.html
files.
<!-- templates/home.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>Home Page</title>
</head>
<body>
<h1>Welcome to the home page!</h1>
</body>
</html>
<!-- templates/about.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>About Page</title>
</head>
<body>
<h1>About Us</h1>
<p>We are a team of developers passionate about web development.</p>
</body>
</html>
Run the Flask application using the following command:
python app.py
Now you can access the home page at http://127.0.0.1:5000
and the about page at http://127.0.0.1:5000/about
.
If you get a TemplateNotFound
error, it is likely that the templates
directory is not in the correct location or the template file name is misspelled.
If your CSS or JavaScript files are not being loaded, check the path in the url_for
function and make sure the static
directory is in the correct location.
If you have two routes with the same URL pattern, Flask will raise an error. Make sure each route has a unique URL.
As your multi - page app grows, it becomes difficult to manage all the routes in a single file. Blueprints allow you to organize your routes into smaller, more manageable modules.
from flask import Flask, Blueprint
# Create a blueprint
about_bp = Blueprint('about', __name__)
@about_bp.route('/about')
def about():
return "This is the about page."
app = Flask(__name__)
app.register_blueprint(about_bp)
if __name__ == '__main__':
app.run(debug=True)
Implement proper error handling in your application. Flask allows you to define custom error handlers for different HTTP status codes.
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
Use secure coding practices such as input validation and protection against common web vulnerabilities like SQL injection and cross - site scripting (XSS).
Creating a multi - page app with Flask is a straightforward process. By understanding the core concepts of routes, templates, and static files, you can build powerful and scalable web applications. We have also explored typical usage scenarios, common pitfalls, and best practices to help you create a robust multi - page app. With Flask’s simplicity and flexibility, you can easily adapt it to different real - world situations.