问题
I've read the official Django Docs and many other posts here in SO. Still I'm unable to figure out the way to load my CSS files in HTML pages. I'm running Django 1.4
. Here is my whole structure of mysite
project having auth
as the app.
mysite/
manage.py
settings.py
__init__.py
urls.py
auth/
__init__.py
forms.py
models.py
views.py
templates/
index.html
home.html
media/
common.css
How can I load my common.css
file in index
and home
template files?
回答1:
Take a look at static files
: https://docs.djangoproject.com/en/dev/howto/static-files/
You need to:
- add
django.contrib.staticfiles
to your installed apps - put your files in the
static
directory - set
STATIC_ROOT
andSTATIC_URL
in your settings - use the
STATIC_URL
variable or thestatic
templatetag in your templates
{% load staticfiles %}
and then <link href="{% static "common.css" %}" rel="stylesheet" type="text/css"/>
In your production environment you'll then need to collect the static files
来源:https://stackoverflow.com/questions/14029730/load-css-images-in-django