Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Templates and Static Files Static Files

css not being applied to add.html

My css is being applied to index.html but not add.html

Layout.html:

<!doctype html>
<html>
    <head>
        <title>{% block title %}Flask Basics{% endblock %}</title>
        <link rel = 'stylesheet' href='static/styles.css'>
    </head>
    <body>
        {% block content %}{% endblock %}
        <p> Brought to you by the fine folks at Treehouse </p>
        {% block scripts %}{% endblock %}
    </body>
</html>

Index.html

{% extends "layout.html" %}

{% block title %}Howdy! | {{ super() }} {% endblock %}
{% block content %}
<h1>Hello from {{name}}</h1>
{% endblock %}

{% block scripts %}
<script src="/static/scripts.js"></script>
{% endblock %}

add.html

{% extends "layout.html" %}

{% block title %}Adding! | {{ super() }} {% endblock %}
{% block content %}
<h1>{{ num1 }} + {{ num2 }} = {{ num1 + num2 }}</h1>
{% endblock %}

1 Answer

Kent Γ…svang
Kent Γ…svang
18,823 Points

For the Jinja2-templating engine to include a stylesheet you must use the function flask.url_for().

Assuming that your file structure is correct, your html-head should look like this:

    <head>
        <title>{% block title %}Flask Basics{% endblock %}</title>
        <link rel = 'stylesheet' href='{{ url_for("static", filename="styles.css) }}'>
    </head>
Edward Humphreys
Edward Humphreys
17,884 Points

Lots of solutions posted about this, but your solution, Kent, was the only one that worked for me (using chrome). Thanks