Django loading css from template

2019-08-24 02:43发布

问题:

okay so my base.html template is in

/home/user/documents/project/app/templates/base.html

and my style.css is in

/home/user/documents/project/app/static/css/style.css

and my consola.ttf font is in

/home/user/documents/project/app/static/fonts/consola.ttf

My static_URL is

STATIC_URL = '/static/'

and my STATIC_ROOT is

STATIC_ROOT = '/home/user/documents/project/app/static'

now, how do I link the style.css to my base.html template? I tried doing

but it didn't work. Also, how do I load the consola.ttf font in my style.css? Since both style.css and the consola.ttf font are in the same static folder, I tried doing

@font-face { font-family: consola; src: url(../../fonts/consola.ttf'); }

but that didn't work either. Any idea on how to fix these two problems?

Here is the beginning of my .html template.

<html>
    <head>
        {% load staticfiles %}
        <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css" />
        <title>{% block title %}Name{% endblock %}</title>
    </head>

that's how it should be, right?

回答1:

Okay found the answer. I was supposed to do

<link rel="stylesheet" type="text/css" href="{% static "css/style.css" %}" />

instead of

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css" />