0

I'm new to Django and I'm trying to display each user's profile. So far, I've made a sign-in, sign-up system. User can also update their profiles but I'm really not able to show each user's profile as soon as the logged in user clicks on the other user's username. I was wondering if I could do something with the primary key/ID of the each profile and use it to get access to every profile. A little help would be really appreciated!

Here's what my code looks like:

The URL to the Profile's page:

path('profile/', views.userprofile, name='userprofile'),

My view to view the profile:

@login_required
def userprofile(request):
    Post = post.objects.order_by('-created')
    return render(request,'social_media/profile.html', {'Post':Post,'Profile':Profile})

Model for user's profile:

class profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    desc = models.CharField(max_length=1000)
    pfp = models.FileField(default='images/profile.jpg', upload_to='profile_pics/', blank=True)
    cover = models.FileField(default='images/profile.jpg', upload_to='cover_pics/', blank=True)

Link that take me to the profile URL:

<div class="user-pfp-name">
    <a href="{% url 'userprofile' %}">
        <img src="{% static 'social_media/images/profile.jpg' %}" style="height: 35px; width: auto; border-radius: 100%;" alt="">
        {{ i.user.first_name }}
        {{ i.user.last_name }} 
    </a>
</div>

and lastly here's my profile page:

      <div class="profile">
            <div class="coverpad">
                  {% if Profile.cover %}
                  <img src="{{ Profile.cover.url }}">
                  {% endif %}
            </div>
            <div class="profilepic">
                  {% if Profile.pfp %}
                  <img src="{{ Profile.pfp.url }}" height="140px">
                  {% endif %}
            </div>
            <div class="profile-middle">
                  <div class="profile-username">{{ user.first_name }} {{ user.last_name }}</div>
                  {% if Profile.desc %}
                  <div class="profile-desc"></div>
                  {% endif %}
            </div>
      </div>
notVansh
  • 207
  • 4
  • 15

2 Answers2

0

You have to pass user's id to your view function. For example:

@login_required
def userprofile(request, user_id):
    user = User.object.get(id=user_id)
    return render(request,'social_media/profile.html', {'user': user})

urls.py

path('profile/<int:user_id>', views.userprofile, name='userprofile'),

link to profile:

<a href="{% url 'userprofile' user_id=user.id %}">

Then your profile page will look like this:

<div class="coverpad">
                  {% if user.cover %} #or user.profile.cover, i'm not sure
                  <img src="{{ user.cover.url }}"> #or user.profile.cover.url
                  {% endif %}
            </div>

Etc.

bkrop
  • 228
  • 2
  • 10
  • That works but everytime I click it, it takes me to same profile_id. For example every profile I click it takes me to http://127.0.0.1:8000/profile/3 – notVansh Aug 25 '20 at 19:12
0

It worked, I just changed my view to this:

@login_required
def userprofile(request, user_id=None):
    if user_id:
        user = User.objects.get(id=user_id)
        Post = post.objects.order_by('-created')
        return render(request,'social_media/profile.html', {'Post':Post,'User':user})
    else:
        user = User.objects.get(id=user_id)
        Post = post.objects.order_by('-created')
        return render(request,'social_media/profile.html', {'Post':Post,'User':user})

So basically if the user requests an ID, I take him to the requested user's ID profile and if the user(logged in) wanna see his/her own profile, I just take him to his/her ID profile.

notVansh
  • 207
  • 4
  • 15