Django如果图像没有从管理员上传添加默认图像

时间:2017-09-05 11:35:38

标签: python django

我的Django图像字段有问题。我应该做图像可选。如果管理员没有上传图片,django应该添加默认图片。怎么办呢?

这是我的代码:

class News(models.Model):
news_title = models.CharField(max_length=255)
news_text = models.TextField(max_length=5000)
news_short_text = models.CharField(max_length=255)
news_logo = models.ImageField(upload_to='uimages', blank=True, null=True, default='')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

提前致谢

1 个答案:

答案 0 :(得分:4)

news_logo = models.ImageField(
    upload_to='uimages', blank=True, null=True,
    default='path to image after your media folder')

将图片添加到字段的default部分