Django 不想显示媒体文件 [英] Django doesn't want to show media files

查看:89
本文介绍了Django 不想显示媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试输出我这样保存的图像:

product_image = models.ImageField(blank = True, upload_to='images')

我的seetings.py看起来像:

MEDIA_URL = '/media/'MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

这里还有 urls.py:

urlpatterns = [路径('管理员/',admin.site.urls),url(r'^$', include('store.urls')),url(r'^', include('store.urls')),] + 静态(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

Query 就像 ORM 的简单实现,但我也在 ORM 上尝试过

def 索引(请求):产品 = DAO.get_all_products()返回渲染(请求,'store/index.html',locals())

HTML:

<img src="{{product.product_image}}" class="img-responsive watch-right" alt=""/>

它不起作用,我没有看到任何图像,{{product.product_image}} 在浏览器中向我显示图像/pic13.jpg.但我可以在/admin 中看到这张图片错误:

未找到:/images/pic13.jpg

如何解决这个问题?此页面位于名为 store 的 App 中,urls.py 和 settings.py 位于主应用程序中

解决方案

我们应该强烈使用 ORM 和查询到 django 模型中,django 不能像使用 ImageField 一样处理来自数据库的字符串.

I'm trying to output an image which i save like this:

product_image = models.ImageField(blank = True, upload_to='images')

My seetings.py looks like:

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Also here is urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^$', include('store.urls')),
    url(r'^', include('store.urls')),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Query is like simple implemetation of ORM, but i tried it on ORM also

def index(request):
products = DAO.get_all_products()
return render(request, 'store/index.html', locals())

HTML:

<img src="{{product.product_image}}" class="img-responsive watch-right" alt=""/>

And it doesn't work, i don't see any image, and {{product.product_image}} shows me images/pic13.jpg in browser. But i can see this image in /admin Error:

Not Found: /images/pic13.jpg

How to fix this issue? This page is situated is App called store, urls.py and settings.py are in main app

解决方案

We should strongly use ORM and queries into django models, django can't work with strings from database like with ImageField.

这篇关于Django 不想显示媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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