问题
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.staticfilesto your installed apps - put your files in the
staticdirectory - set
STATIC_ROOTandSTATIC_URLin your settings - use the
STATIC_URLvariable or thestatictemplatetag 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