在Django中加载CSS /图像

时间:2012-12-25 10:19:05

标签: css django django-models django-templates django-views

我已经在SO中阅读了官方Django文档和许多其他帖子。我仍然无法弄清楚在HTML页面中加载CSS文件的方法。我正在运行Django 1.4。以下是mysite项目的整个结构,auth为应用。

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

如何在common.cssindex模板文件中加载home文件?

1 个答案:

答案 0 :(得分:3)

查看static fileshttps://docs.djangoproject.com/en/dev/howto/static-files/

你需要:

  1. django.contrib.staticfiles添加到已安装的应用
  2. 将您的文件放在static目录
  3. 在您的设置中设置STATIC_ROOTSTATIC_URL
  4. 使用模板中的STATIC_URL变量或static模板标签
  5. {% load staticfiles %}然后<link href="{% static "common.css" %}" rel="stylesheet" type="text/css"/>

    在生产环境中,您需要收集静态文件