Template Inheritance in Flask
×


Template Inheritance in Flask

1260

How to Serve Static Files in Flask

Introduction

When building web applications with Flask, you'll often need to include static assets like CSS, JavaScript, or images. Flask makes this easy by providing a built-in mechanism to organize and serve static files. In this guide, I'll walk you through organizing these assets and integrating them smoothly into your project.

Project Organization

Begin by creating a folder named static alongside your Flask app. Inside it, you can categorize assets, for instance:

  • static/css/style.css
  • static/js/script.js
  • static/images/logo.png

Referencing Files in Templates

Use Flask’s url_for function inside your templates to generate correct URLs for your assets:

<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
<script src="{{ url_for('static', filename='js/script.js') }}"></script>
<img src="{{ url_for('static', filename='images/logo.png') }}" alt="Logo">

Flask App Setup

Here’s a base app setup demonstrating serving a template that uses these static resources:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

Example Template

Inside templates/index.html, include your static files:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Home ‑ My Flask App</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <h1>Welcome to My Flask Site</h1>
    <img src="{{ url_for('static', filename='images/logo.png') }}" alt="Logo">
    <script src="{{ url_for('static', filename='js/script.js') }}"></script>
</body>
</html>

Configuration Options

By default, Flask serves files in the static directory at URLs under /static/<filename>. If needed, you can change this folder or URL prefix when initializing your app:

app = Flask(__name__, static_folder='assets', static_url_path='/resources')

Why Use This Approach?

  • Organization: Keeps your front-end assets neatly managed.
  • Simplicity: Flask automatically handles serving these files in debug mode.
  • Flexibility: Easily switch directories or URL paths when deploying.

Testing Locally

With debug=True, Flask serves static files out of the box. Navigate to http://127.0.0.1:5000/ to see everything loaded correctly. When you deploy to production, ensure your web server is configured to serve static assets efficiently.

Conclusion

“How to serve static files in Flask” covers setting up a structured static folder, referencing assets via url_for, and customizing paths if necessary. This approach ensures a clean, maintainable workflow for including CSS, JS, and images in any Flask application.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat