Default image for ImageField in Django's ORM

后端 未结 6 1755
夕颜
夕颜 2020-12-03 01:22

I\'m using an ImageField to store profile pictures on my model.

How do I set it to return a default image if no image is defined?

6条回答
  •  悲&欢浪女
    2020-12-03 01:27

    I haven't tried this, but I'm relatively sure you can just set it as a default in your field.

    pic = models.ImageField(upload_to='blah', default='path/to/my/default/image.jpg')
    

    EDIT: Stupid StackOverflow won't let me comment on other people's answers, but that old snippet is not what you want. I highly recommend django-imagekit because it does tons of great image resizing and manipulation stuff very easily and cleanly.

提交回复
热议问题