Django中static、media与template设置

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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值