1

I have s small website, but the icon doesn't work properly: Here's the code:

base.html

<html>
    <head>
        <link rel="icon" href="static/icon.png" sizes="32x32">
        <title>
            {% block title %}{{title}}{% endblock %}
        </title>
        <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div id="page_container">
            {% block content %}
            <div class="container-fluid" style="margin-top: 100px;">
                {% with messages = get_flashed_messages() %}
                {% if messages %}
                    {% for message in messages %}
                    <div class="alert alert-info" role="alert">{{message}}</div>
                    {% endfor %}
                {% endif %}
                {% endwith %}

                {# application content needs to be provided in the app_content block #}
                {% block app_content %}{% endblock %}
            </div>
            {% endblock %}
        </div>
    </body>
</html>

Browser shows the icon on this template:

about.html

{% extends "base.html" %}
{% block app_content %}
<div id="about_author_photo">
    <img src="static/VVphoto.jpg" height="400px" width="270px">
</div>
{% endblock %}

but it doesn't on the one like this:

poems.html

{% extends "base.html" %}
{% block app_content %}

    {% for poem in poems.items %}
        {% include "_poem.html" %}
    {% endfor %}

    <div class="pagination-links">
    {% for page in poems.iter_pages() %}
        {% if page %}
            {% if page != poems.page %}
                <a href="{{ url_for('poems', page_num=page) }}">{{ page }}</a>
            {% else %}
                <span style="color: white;">({{ page }})</span>
            {% endif %}
        {% else %}
            <span class=ellipsis style="color: white;">…</span>
        {% endif %}
    {% endfor %}
    </div>

{% endblock %}

Here are the views for both of them:

@app.route('/')
@app.route('/about')
def about():
    return render_template('about.html', title='З Гумором Про Все На Світі')


@app.route('/poems/<int:page_num>')
def poems(page_num):
    poems = Poem.query.paginate(per_page=5, page=page_num, error_out=True)
    return render_template('poems.html', title='Вірші',  poems=poems)

i tried adding the icon to the poems tepmlate under {% extends "base.html %} line but it did not work

2 Answers2

2

The icon url is relative at the moment. You should use {{ url_for('static', filename='icon.png') }}

More info https://flask.palletsprojects.com/en/1.1.x/tutorial/static/

oittaa
  • 599
  • 3
  • 16
1

Your icon location is using a path relative to your current page.

<link rel="icon" href="static/icon.png" sizes="32x32">

On the about or home page, you would have domain.com/ or domain.com/about as your url. That will cause the browser to look for the icon at domain.com/static/icon.png. On the poems page, it looks like you will always be on a specific page, like domain.com/poems/1, which makes the relative path domain.com/poems/static/icon.png.


To ensure the path is always relative to the root of the domain, prefix the path with a / so it should instead look like this in your base.html

<link rel="icon" href="/static/icon.png" sizes="32x32">
quittle
  • 856
  • 2
  • 10
  • 19