Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,822 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