欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Django media static外部访问Django中的图片设置教程

时间:2021-05-03 10:03:55|栏目:Python代码|点击:

有两种方式:一种是图片放在static中,一种是图片放在media中

第一种:

即:

STATIC_URL = '/static/'
STATICFILES_DIRS=[
  os.path.join(BASE_DIR, "static")
]

运行项目然后

第二种方式:

用media这个文件存储图片,音视频等文件

补充知识:django {{ MEDIA_URL }}值为空

在模板中:

<a href="#nowhere" rel="external nofollow" ><img width="100px" src="{{ MEDIA_URL }}{{ product.productImages.all.first.image }}"></a> {# img/productSingle_middle/676.jpg #}

结果{{ MEDIA_URL }}为空

解决方法:

在settings.py的TEMPLATES 中添加:

django.template.context_processors.media

TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR,'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
      'context_processors': [
        'django.template.context_processors.debug',
        'django.template.context_processors.request',
        'django.contrib.auth.context_processors.auth',
        'django.contrib.messages.context_processors.messages',
        'django.template.context_processors.media',添加这行或者下面一行
        #'django.core.context_processors.media',python3会报错
      ],
    },
  },
]

上一篇:Python3.5面向对象程序设计之类的继承和多态详解

栏    目:Python代码

下一篇:python按照多个字符对字符串进行分割的方法

本文标题:Django media static外部访问Django中的图片设置教程

本文地址:http://www.codeinn.net/misctech/114139.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有