I am able to upload a profile picture and it goes to the database. I have a “print(profile.profile_picture)” in my views.py that will correctly print the current profile picture in the console when I save. For some reason the profile picture will not show though, my “{% if profile.profile_picture %}” in my profile.html isn’t even picking anything up.
[]
VIEWS.PY=
login_required
def profile(request):
profile, created = Profile.objects.get_or_create(user=request.user)
print(profile.profile_picture)
if request.method == 'POST':
form = ProfileForm(request.POST, request.FILES, instance=profile)
if form.is_valid():
form.instance.user = request.user
form.save()
return redirect('base:profile') # Redirect to the home page after form submission
else:
form = ProfileForm(instance=profile)
return render(request, 'profile.html', {'form': form})
PROFILE.HTML=
<div class="ProfilePage">
<form method="POST" enctype="multipart/form-data" class="profile-form">
{% csrf_token %}
<div class="form-group">
<label for="id_username">Username:</label>
<input type="text" id="id_username" value="{{ request.user.username }}" readonly>
</div>
<div class="form-group">
<label for="id_bio">Bio:</label>
<textarea id="id_bio" name="bio" rows="4" cols="50" required>{{ form.bio.value }} </textarea>
</div>
<div class="form-group">
<label for="id_profile_picture">Profile Picture:</label>
<input type="file" id="id_profile_picture" name="profile_picture" accept="image/*">
</div>
{% if profile.profile_picture %}
<div class="form-group">
<label>Profile Picture Preview:</label><br>
<img src="{{ profile.profile_picture.url }}" alt="Profile Picture" style="max-width: 500px;">
</div>
{% endif %}
<button type="submit" class="btn-submit">Save</button>
</form>
</div>
FORMS.PY=
from django import forms
from .models import Profile
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['bio', 'profile_picture']
I can give more information, I’m a bit confused why this isn’t working.