MessitÖzil
MessitÖzil

Reputation: 1378

Django-- Get model fields in template only for current user

First of all, I know there are lot of questions about accessing model in template but let me explain why this is different.

I want profile page where User can see their details. I am able to do that but with one little bug. If there are 3 Users(say A, B,C) and User A want to see his profile he sees it three time. Like this:

like this

How do I stop the loop after one iteration so that the User only get's his profile information once.

This is my URL:

url(r'^profile/$', views.IndexView.as_view(), name='profile'),

Views.py:

class IndexView(generic.ListView):
template_name = 'profile.html'

def get_queryset(self):
    return Profile.objects.all()

and profile.html:

{% if user.is_authenticated %}
{% for profile in object_list %}
<h1>{{ request.user }}'s Profile</h1>
<table>
<tr>
    <td>Username:</td>
    <td>{{ user.username }}</td>
</tr>
<tr>
    <td>First Name:</td>
    <td>{{ user.first_name }}</td>
</tr>
<tr>
    <td>Last Name:</td>
    <td>{{ user.last_name }}</td>
</tr>
<tr>
    <td>Email:</td>
    <td>{{ user.email }}</td>
</tr>
<tr>
    <td>Personal Info:</td>
    <td>{{ user.profile.personal_info }}</td>
</tr>
<tr>
    <td>Job Title:</td>
    <td>{{ user.profile.job_title }}</td>
</tr>
<tr>
    <td>Department:</td>
    <td>{{ user.profile.department }}</td>
</tr>
<tr>
    <td>Location:</td>
    <td>{{ user.profile.location }}</td>
</tr>
<tr>
    <td>Expertise:</td>
    <td>{{ user.profile.expertise }}</td>
</tr>
<tr>
    <td>Phone Number:</td>
    <td>{{ user.profile.phone_number }}</td>
</tr>
<tr>
    <td>Contact Skype:</td>
    <td>{{ user.contact_skype }}</td>
</tr>
<tr>
    <td>Contact Facebook:</td>
    <td>{{ user.contact_facebook }}</td>
</tr>
<tr>
    <td>Contact Linkedin:</td>
    <td>{{ user.profile.contact_linkedin }}</td>
</tr>
</table>

<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {% include 'form-template.html' %}
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <a href={%  url 'profile_edit' %}><input type="button" class = " col-sm-offset-2 btn bk-bs-btn-warning " name="cancel" value="Edit" /></a>
            </div>
        </div>
</form>
{% endfor %}
{% else %}
    <h2>Please login to see your Profile</h2>
{% endif %}

I am a newbie to django, thanks is advance.

Upvotes: 0

Views: 280

Answers (1)

NS0
NS0

Reputation: 6126

You're looping over all Profile's but not actually using this data in the loop, instead you're using user.profile.

This means that you likely have 3 Profile objects in the database, then for each you display the details of the current user, which isn't desirable.

So you can remove the loop entirely, and just keep its contents that refer to all the user.profile attributes to achieve the desired result.


edit

Looks like user is passed into your template by default, and points to the currently logged in user. So user.profile will return the profile, and this works without doing anything else. So user.profile.personal_info is valid and should work. When you tried to use profile directly, that is not the same thing, and wasn't defined, so profile.personal_info didn't work. Then in your loop you used profile to loop over Profile objects, but this wasn't necessary or was used. Hope this makes sense.

Upvotes: 1

Related Questions