惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
博客园 - 司徒正美
美团技术团队
Last Week in AI
Last Week in AI
月光博客
月光博客
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
U
Unit 42
T
Tailwind CSS Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
H
Help Net Security
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
Y
Y Combinator Blog
罗磊的独立博客
D
DataBreaches.Net
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
博客园_首页
博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
博客园 - Franky
M
MIT News - Artificial intelligence
B
Blog
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
S
SegmentFault 最新的问题
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog

博客园 - guofu

Python爬虫之BeautifulSoap的用法 pip使用豆瓣的镜像源 Android 中的 Service 全面总结 【转】IT 圈里有哪些经常被读错的词? Android开发的技术层次 ubuntu下安装oracle 网站框架策划时的小技巧--页面原型篇 中国电商价格欺诈何时休? Mac OS X上如何实现到Linux主机的ssh免登陆[forward] 25个必须记住的SSH命令[forward] 详细介绍ORACLE sqlplus命令(转) Oracle 数据库导入导出命令 "The test form is only available for requests from the local machine"解决方法 JAVA学习网站收集 解决Eclipse中ISO8859-1 字符集的方法 Windows 如何在cmd命令行中查看、修改、删除与添加环境变量 Bugzilla安装过程 有关ant的faq Win7或Windows server 2008中IIS7支持ASP+Access解决方法
Django Media URL 文件上传 配置
guofu · 2020-06-30 · via 博客园 - guofu

如果在url中没有配置media,即使在settings设置了MEDIA_ROOT,也无法正常显示图片。

会遇到如下错误

Using the URLconf defined in me_blog.urls, Django tried these URL patterns, in this order:

    blog/
    admin/

The current path, media/banner/发货-2.jpg, didn't match any of these.

如何解决呢?需要一次做出如下操作:

1.正确配置MEDIA_URL和MEDIA_ROOT

settings中配置。

MEDIA_URL = '/media/'
# 设置上传文件的路径
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')   # 指定根目录

 2.在Model中应用配置

class ProductInfo(models.Model):
    # 商品模型:pic为该商品的图片在项目中存储的相对路径
    pic = models.ImageField(verbose_name="图片路径", default="image/default.png", upload_to='df_goods/image/%Y/%m', null=True, blank=True)  # 商品图片

 3.配置url路由

from django.views.static import serve  # 上传文件处理函数

from .settings import MEDIA_ROOT  # 从配置中导入MEDIA_ROOT


urlpatterns = [
    url(r'^media/(?P<path>.*)$', serve, {"document_root":MEDIA_ROOT})
]

 4.配置templates模板

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',  # 将media_url上传文件路径注册到模板中
            ],
        },
    },
]

 5.在页面中使用

<img src="{{ MEDIA_URL }}{{ product.pic }}">
其中{{ product.pic }}为商品的路径

页面经过渲染之后,在浏览器中会显示为:

<img src="/media/product/image/2019/05/product_detail.jpg">

posted on 2020-06-30 13:18  guofu  阅读(675)  评论()    收藏  举报