Django中static、media与template设置

来源:互联网 发布:深圳sem优化 编辑:程序博客网 时间:2024/06/05 06:46

static:

贴一段settings中的代码及注释

PROJECT_ROOT = os.path.abspath(os.path.dirname(__name__))# Absolute path to the directory static files should be collected to.# Don't put anything in this directory yourself; store your static files# in apps' "static/" subdirectories and in STATICFILES_DIRS.# Example: "/home/media/media.lawrence.com/static/"STATIC_ROOT = PROJECT_ROOT+'static/'#使用collectstatic命令时,会将所有collect出来的文件放入该路径下,该路径不能包含在STATICFILES_DIRS中# URL prefix for static files.# Example: "http://media.lawrence.com/static/"STATIC_URL = '/static/'#访问static的前缀# Additional locations of static files# STATICFILES_DIRS是私人的static(也就是不按django命名规则做的static),django在寻找static时会先寻找app下的static,然后再在STATICFILES_DIRS中找一遍STATICFILES_DIRS = (    # Put strings here, like "/home/html/static" or "C:/www/django/static".    # Always use forward slashes, even on Windows.    # Don't forget to use absolute paths, not relative paths.    os.path.join(PROJECT_ROOT,"app_name/static"),    #不建议使用相对路径,使用join将相对路径优化成绝对路径)

DEBUG模式下Static的使用

1、在setting中,把STATICFILES_FINDERS的AppDirectoriesFinder开启
AppDirectoriesFinder会自动查找INSTALLED_APPS中已添加的app下的static文件夹
2、app下的static中创建一个与app同名的文件夹,在文件夹中存放css或js等静态文件
3、html中使用

{% load static %}<link rel="stylesheet" type="text/css" href="{% static 'app/style.css' %}" />

4、图片使用
css同级目录下建立images文件夹,文件夹内存图片

background: white url("images/background.gif")

media:

PROJECT_ROOT = os.path.abspath(os.path.dirname(__name__))MEDIA_PATH = PROJECT_ROOT+"media/"

template:

PROJECT_ROOT = os.path.abspath(os.path.dirname(__name__))TEMPLATE_DIRS = (    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".    # Always use forward slashes, even on Windows.    # Don't forget to use absolute paths, not relative paths.    os.path.join(PROJECT_ROOT,"app_name/templates"),)
0 0
原创粉丝点击