Load CSS/images in Django

做~自己de王妃 提交于 2020-01-05 02:31:08

问题


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:

  1. add django.contrib.staticfiles to your installed apps
  2. put your files in the static directory
  3. set STATIC_ROOT and STATIC_URL in your settings
  4. use the STATIC_URL variable or the static 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!