html上的Django imageField网址 [英] Django imageField url on html

查看:36
本文介绍了html上的Django imageField网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小伙子

-mega_series
   -mega_series
   -series
      -models.py
      -views.py
      ...
   -manage.py
   -media
      -covers
   -static

MEDIA_URL和MEDIA_ROOT如下:

The MEDIA_URL AND MEDIA_ROOT are the following:

MEDIA_ROOT = '/Users/lechucico/Documents/mega_series/media/'
MEDIA_URL = 'media/' 

在模型上,我像这样存储图像:

On the model I store the image like that:

class Serie (models.Model):
   serie_cover = models.ImageField(upload_to='covers')
   serie_name = models.CharField(max_length=100)

通过管理网站上传的图片可以正确转到以下路径:mega_series/media/covers/

The images once uploaded via admin site goes correctly to path: mega_series/media/covers/

这就是我通过html访问它的方式:

And this is how I acces it via html:

<img src="{{serie.serie_cover.url}}" width="150" height="250" />

我得到的结果网址如下:

The result url that I got is the following:

http://127.0.0.1:8000/media/covers/juego_de_tronos.jpg

为什么图片没有出现在html上?

Why the image doesn't appear on the html?

谢谢.

推荐答案

您需要定义 MEDIA_ROOT ,如来自Django官方文档:

From Django officaal Docs:

urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

您还可以查看.

这篇关于html上的Django imageField网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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