jephthah
jephthah

Reputation: 9

how to display user profile data by id in django

was trying to link the user of a profile to a post they made in django something like post.author.profile

here's what ive tried:

views.py

def user(request, id):
if request.method == 'POST':
    r_form = upload_resume(request.POST, request.FILES, instance=request.user.profile)
    if r_form.is_valid():
        r_form.save()
        messages.success(request, f'your resume has been uploaded')
        return redirect('user')
else:
    r_form = upload_resume(instance=request.user)

context = {
    'r_form': r_form
}
return render(request, 'users/profile.html', context)

models.py

class profile(models.Model):
    user = models.OneToOneField(User,on_delete = models.CASCADE, related_name='profile')
    bio = models.TextField(blank=True)

urls.py

 path('user/<int:id>/', views.user, name='user')

and finally html of the profile page

<div class="profile-card__name" style="color: #212f3c;">@{{user.get_username}}</div>
<div class="profile-card__txt"style="text-align: center;">last seen on {{user.last_login}}</div><br>
{%if user.first_name and user.last_name%}
<div class="text" style="font-size: 15px;display: flex;-webkit-box-align: center;-ms-flex-align: center;align-items: center;position: relative; align-items: flex-start;padding-left: 20px;"><img class="user_icons" src="{{object.profile.profile_pic.url}}" style="border-radius: 100%;height: 20px;width: 25px;"><b style="padding-left: 10px;">{{user.first_name}} {{user.last_name}}</b></div>
{%endif%}
{%if object.profile.bio%}
<div class="profile-card__txt" style="position: relative;align-items: flex-start;padding-left: 20px;"><b>About me:</b> {{object.profile.bio}}</div>
{%endif%}
{%if object.profile.current_location%}
<div class="text" style="font-size: 15px;display: flex;-webkit-box-align: center;-ms-flex-align: center;align-items: center;position: relative; align-items: flex-start;padding-left: 20px;"><img class="user_icons" src="{% static 'blog/location.svg'%}" style=" height: 25px;width: 25px;padding-right: 5px;"><b>{{object.profile.current_location}}</b></div>
{%endif%}

when ever i go to user/25/ it renders the information of the current logged in user even with the id of another user..how do i fix this..Thanks in advance :)

Upvotes: 1

Views: 2539

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476729

Well you use the request.user.profile, instead of the profile with the given id. You can use get_object_or_404(…) [Django-doc]:

from django.shortcuts import get_object_or_404

def user(request, id):
    profileobj = get_object_or_404(profile, pk=id)
    if request.method == 'POST':
        r_form = upload_resume(request.POST, request.FILES, instance=profileobj)
        if r_form.is_valid():
            r_form.save()
            messages.success(request, f'your resume has been uploaded')
            return redirect('user', id=id)
    else:
        r_form = upload_resume(instance=profileobj)

    context = {
        'r_form': r_form,
        'object': profileobj.profile
    }
    return render(request, 'users/profile.html', context)

Note: Models in Django are written in PerlCase, not snake_case, so you might want to rename the model from profile to Profile.


Note: Usually a Form or a ModelForm ends with a …Form suffix, to avoid collisions with the name of the model, and to make it clear that we are working with a form. Therefore it might be better to use UploadResumeForm instead of upload_resume.

Upvotes: 2

Related Questions