在Django中加载CSS /图像 [英] Load CSS/images in Django

查看:154
本文介绍了在Django中加载CSS /图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里读过官方的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

如何加载我的 c code code code $ c> c $ c> b $ b

解决方案

看看静态文件 https://docs.djangoproject.com/en/dev/howto/static-files/



您需要:


  1. 添加 django.contrib.staticfiles 到您安装的应用程序

  2. 将您的文件放入静态目录

  3. 设置 STATIC_ROOT STATIC_URL
  4. 使用 STATIC_URL 变量或 static templatetag

{%load staticfiles%} 然后< link href ={%staticcommon.css%} =stylesheettype =text / css/>



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


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?

解决方案

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

这篇关于在Django中加载CSS /图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆