Hi, I am creating an app where users have a profile page that is supposed to display the number of pets they have registered, a profile picture and a bio. However, when I upload a picture, there is no associated photo on the user profile. Some code snippets can be found below:
Here is the code for the UserProfile model:
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(upload_to='profile_images', blank=True)
bio = models.TextField(blank=True)
def __str__(self):
return f"{self.user.username}'s Profile"
Here is the associated view containing the logic for the profile view:
def profile_view(request):
profile = request.user.userprofile
pets = Pet.objects.filter(user=request.user).count()
if request.method == 'POST':
form = UserProfileForm(request.POST, request.FILES, instance=profile)
if form.is_valid():
form.save()
return redirect('profile')
else:
form = UserProfileForm(instance=profile)
return render(request, 'core/profile.html', {'form': form, 'pets': pets})
And finally, here is the template logic that I have set up:
<img src="{{ profile.image.url }}" alt="Profile Picture" class="w-32 h-32 rounded-full mb-4">
{% else %}
<div class="w-32 h-32 rounded-full bg-gray-300 mb-4"></div>
{% endif %}
TYIA