简体   繁体   English

使用 Django 的 ImageField 的问题

[英]Problems using Django's ImageField

I defined the following model:我定义了以下模型:

class Pizza(models.Model):
    name = models.CharField(max_length=50, blank=False, null= False, unique = True)
    description = models.CharField(max_length=100, blank=False, null= False, unique = True)    
    price = models.ForeignKey('Type')    
    image = models.ImageField(upload_to='images', blank=True, null=True)

I use the following parameters on settings/py:我在 settings/py 上使用以下参数:

# Absolute filesystem path to the directory that will hold user-uploaded files.
MEDIA_ROOT = '/my/files/root/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

The file is properly uploaded in:该文件已正确上传到:

/my/files/root/images

However, I reference it like this:但是,我是这样引用的:

<img src="{{ MEDIA_URL }}{{ pizza.image }}"></img>

... and the link is broken. ......链接断开了。

The link shows:链接显示:

/media/images/imagename.jpg

What can I be missing?我能缺少什么?

尝试这个:

{{ pizza.image.url }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM