Why my profile model is not working in django?

rakin235

I want to make profiles of users using Django. On the profile page, users can see the profile pictures and posts they've made. Although users can see their posts, they can hardly view their profile pictures. Here I am sharing my profile model and other views.py functions related to that model.

models.py

class Profile(models.Model):
     user = models.OneToOneField(User, on_delete = models.CASCADE)
     picture = models.ImageField(null=True,blank=True,upload_to="images/")

views.py

def profilepage(request,id):
    user = User.objects.get(pk=id)
    profile = Profile.objects.filter(user = user)
    posts = NewPost.objects.filter(user = user).order_by("-timestamp")
    return render(request, "network/profile.html",{
        "user" : user,
        "profile" : profile,
        "media_url" : settings.MEDIA_URL,
        "posts" : posts
    })

If I use this function in my template, I can't get the profile picture. If I write my views in the following way, I can get the profile picture but that isn't of the corresponding user. That is, a user views other people's profile pictures on their own profile.

def profilepage(request,id):
    user = request.user
    profile = Profile.objects.get(pk = id)
    posts = NewPost.objects.filter(user = user).order_by("-timestamp")
    return render(request, "network/profile.html",{
        "user" : user,
        "profile" : profile,
        "media_url" : settings.MEDIA_URL,
        "posts" : posts
    })

This function causes no matching with user id and profile picture. I have been trying to relate the User model and Profile model for a couple of days but couldn't fix it. Here is my HTML template.

<img src="{{ MEDIA_URL }}{{ profile.picture }}" alt="image not found." style="border-radius: 50%;" height="100px" width="100px">

Sorry for my bad English.

rakin235

The main problem was with my HTML template. I should write {{ user.profile.picture.url }} instead of {{ MEDIA_URL }}{{ profile.picture }} in my src attribute. Then it would work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why my django signals not working for User model?

Why is my href link to Facebook profile not working?

Why is my for loop not working in django?

Why is my OData POST body to model not working?

Django profile model

Why is my Django model missing the values attribute?

Why Django doesnt migrate my altered model?

Why the field description of my django model is not null?

Why will my django model data not display in html

Why my optional:true option is not working in my Rails model?

Why is my chained Django queryset not working?

why my search bar is not working in Django?

Why my function in django to change a BooleanField is not working?

How do I retain my `profile_pic` when I update other fields in my Profile model in django?

Django model with User, Profile and company

Adding a profile image in django is not working

Why is setting alias in .profile not working?

Why isn't my Model Query set working properly?

Why is Model Binding not working in my POST action method?

Why isn't my mongoose validation method on the model not working with Joi?

Why isn't my Django User Model's Password Hashed?

why my submit button's not working in updateview in Django?

Why isn't mobile detection enabled working in my Django template?

Why Isn't My NodeJS Post Request Working? (From Django)

Why is my SIGNAL not working in Django - what I'm doing wrong?

Why is my else statement not working in django rest framework?

Why is my django web-app login form not working?

Django model property is not working - Django

curl not working for linkedin profile of my connections