Django The 'image' attribute has no file associated with it

后端 未结 7 941
萌比男神i
萌比男神i 2020-12-01 07:09

When a user registers for my app.I receive this error when he reaches the profile page.

The \'image\' attribute has no file associated with it.
Exception Typ         


        
相关标签:
7条回答
  • 2020-12-01 08:03

    My dear friend, others solvings are good but not enough because If user hasn't profile picture you should show default image easily (not need migration). So you can follow below steps:

    Add this method to your person model:

    @property
    def get_photo_url(self):
        if self.photo and hasattr(self.photo, 'url'):
            return self.photo.url
        else:
            return "/static/images/user.jpg"
    

    You can use any path (/media, /static etc.) but don't forget putting default user photo as user.jpg to your path.

    And change your code in template like below:

    <img src="{{ profile.get_photo_url }}" class="img-responsive thumbnail " alt="img">
    
    0 讨论(0)
提交回复
热议问题