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
It looks like there might be a mismatch between the variable names used in your view function and the variable names used in your HTML template. In your view function, you are passing the user object using the key 'user_image', but in your template, you are trying to access it using user. if you are passing 'user_image' should use image 'user_image.user_image.url' in template
Answered By - shamseer tt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.