Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13921

Profile Picture Not Showing

$
0
0

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.[media]

printed profile picture

profile picture

VIEWS.PY=

@login_requireddef 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'] 

URLS.PY =

    from django.contrib import admin    from django.urls import path, include    from django.conf import settings    from django.conf.urls.static import static    from base import views  # Import views from the base app    urlpatterns = [    path('admin/', admin.site.urls),    path("", include(("base.urls", "base"), namespace="base")),  # Include      app-level URLs with a namespace    path('profile/settings/', views.profile_settings, name='profile_settings'),    path('profile/<str:username>/', views.public_profile, name='public_profile'),] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

MODELS.PY =

    from django.db import models    from django.contrib.auth.models import User    from django.contrib.auth import get_user_model    import uuid      from datetime import datetime    User = get_user_model()    class Profile(models.Model):          user = models.OneToOneField(User, on_delete=models.CASCADE)          bio = models.TextField(blank=True)          profile_picture = models.ImageField(upload_to='profile_pics',        blank=True)     class Post(models.Model):          id = models.UUIDField(primary_key=True, default=uuid.uuid4)          user = models.CharField(max_length=100)          image = models.ImageField(upload_to='post_images')          caption = models.TextField()          created_at = models.DateTimeField(default=datetime.now)          no_of_likes = models.IntegerField(default=0)          def __str__(self):                return self.user

I can give more information, I'm a bit confused why this isn't working.


Viewing all articles
Browse latest Browse all 13921

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>