Issue
When a user logs in, I want to see their profile picture if they have one; if not, the default profile picture should be displayed.
Here is my view.py
def header(request):
if request.session.has_key('id'):
user_id = request.session['id']
user = registertb.objects.get(id=user_id)
return render(request, 'headerfooter.html', {'user_image': user})
else:
return render(request, 'headerfooter.html')`
```
HTML CODE
```
<li>
<p style="width:30px; height:30px; margin-left: 200px; margin-top: 10px;color: black;">{{ i.User_name }}</p>
{% if user.user_image %}
<img src="{{ i.user_image.url }}" style="height: 50px; width: 50px;" alt="Default Image">
{% else %}
<img src="/media/image/New folder/default-avatar-profile.jpg" style="height: 50px; width: 50px;" alt="Default Image">
{% endif %}
</li>`
```
models.py
```
class registertb(models.Model):
User_name=models.CharField(max_length=300)
User_email=models.CharField(max_length=300)
user_image=models.ImageField(upload_to='image/')
password=models.CharField(max_length=400)
conf_password=models.CharField(max_length=300)
`
```
Solution
""" def user_image(request): user_img = None
if 'id' in request.session:
user_id = request.session['id']
try:
user = registertb.objects.get(id=user_id)
if user.user_image:
user_img = user.user_image.url
except registertb.DoesNotExist:
pass
return {'user_img': user_img}
""" try this, it will work
Answered By - SOURAV P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.