124๐
bob
and person
are the same object,
person = Person.objects.get(user=request.user)
bob = Person.objects.get(user=request.user)
So you can use just person for it.
In your template, check image
exist or not first,
{% if person.image %}
<img src="{{ person.image.url }}">
{% endif %}
96๐
The better approach which would not violate DRY is to add a helper method to the model class like:
@property
def image_url(self):
if self.image and hasattr(self.image, 'url'):
return self.image.url
and use default_if_none template filter to provide default url:
<img src="{{ object.image_url|default_if_none:'#' }}" />
- [Django]-Where is a good place to work on accounts/profile in Django with the Django registration app?
- [Django]-Explicitly set MySQL table storage engine using South and Django
- [Django]-CSRF Failed: CSRF token missing or incorrect
15๐
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">
- [Django]-Python Socket.IO client for sending broadcast messages to TornadIO2 server
- [Django]-How to perform OR condition in django queryset?
- [Django]-Laravel's dd() equivalent in django
6๐
Not exactly what OP was looking for, but another possible solution would be to set a default value for ImageField:
class Profile(models.Model):
# rest of the fields here
image = models.ImageField(
upload_to='profile_pics/',
default='profile_pics/default.jpg')
- [Django]-Dynamically adding a form to a Django formset
- [Django]-Reducing Django Memory Usage. Low hanging fruit?
- [Django]-Create empty queryset by default in django form fields
5๐
you have two choices :
- first one:
in the model field try to put a default image value , like this :
PRF_image = models.ImageField(upload_to='profile_img', blank=True, null=True , default='profile_img/925667.jpg')
- the second one (recommended) :
add a custom method inside your class model like the following , to return PRF_image url if exist or return empty string if not :
PRF_image = models.ImageField(upload_to='profile_img', blank=True, null=True )
@property
def my_PRF_image(self):
if self.PRF_image :
return self.PRF_image.url
return ''
and inside your template you can use :
{{ your_object.my_PRF_image }}
i hope this helpful .
- [Django]-Django character set with MySQL weirdness
- [Django]-Python Django Rest Framework UnorderedObjectListWarning
- [Django]-Django ManyToMany filter()
3๐
You can also use the Python 3 built-in function getattr to create your new property:
@property
def image_url(self):
"""
Return self.photo.url if self.photo is not None,
'url' exist and has a value, else, return None.
"""
if self.image:
return getattr(self.photo, 'url', None)
return None
and use this property in your template:
<img src="{{ my_obj.image_url|default_if_none:'#' }}" />
- [Django]-Storing an Integer Array in a Django Database
- [Django]-What is the purpose of adding to INSTALLED_APPS in Django?
- [Django]-How to dynamically compose an OR query filter in Django?
2๐
Many way to solve this issue
Try below code
models.py # under Person class
@property
def imageURL(self):
if self.image:
return self.image.url
else:
return 'images/placeholder.png'
html file
<img src="{% static person.imageURL %}" class="thumbnail" />
- [Django]-How can I filter a Django query with a list of values?
- [Django]-Creating a dynamic choice field
- [Django]-Django URL Redirect
1๐
Maybe this helps but my database didnโt save on of the pictures for the object displayed on the page.
As that object in models.py has blank=False and also I am looping through object, it constantly gave an error until I added a replacement picture in the admin for the database to render.
- [Django]-Django error: got multiple values for keyword argument
- [Django]-Storing an Integer Array in a Django Database
- [Django]-How to save pillow image object to Django ImageField?
0๐
This error also arises when any one or more items doesnโt have an image added and the rest items do. To fix this:
class Product(models.Model):
pname = models.CharField(max_length=30)
price = models.IntegerField()
img = models.ImageField(null = True,blank = True)
def __str__(self):
return self.pname
@property
def imageURL(self):
try:
url = self.img.url
except:
url=''
return url
- [Django]-Django template how to look up a dictionary value with a variable
- [Django]-How to submit form without refreshing page using Django, Ajax, jQuery?
- [Django]-Add Text on Image using PIL
0๐
I had a similar problem , but my issue was with the form in HTML template. if you donโt set the enctype = "multipart/form-data" attribute then it does not upload the image hence the reason for the error
- [Django]-WSGI vs uWSGi with Nginx
- [Django]-How do I render jinja2 output to a file in Python instead of a Browser
- [Django]-How does the get_or_create function in Django return two values?