0

I've extended the user model with the following model:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    designs = models.ManyToManyField('Design', blank=True)
    prints = models.ManyToManyField('Print', blank=True)
    rating = models.IntegerField(null=True, blank=True)
    reliability = models.IntegerField(null=True, blank=True)
    av_lead_time = models.IntegerField(null=True, blank=True)

I've verified that this OneToOne field works because it's included in the user fields in admin. Now I need to use it to create an html table of user fields (both User and UserProfile). I'm trying the following code, but I don't get anything returned:

user_list = User.objects.all()
    return render_to_response('index.html', {
        'user_list': user_list    
    })

template:

{% for user in user_list %}
    <tr>
        <td>{{ user.rating }}</td>
        <td>{{ user.reliability }}</td>
        <td>4</td>
        <td>2</td>
        <td>96</td>
        <td>£4.56</td>
        <td class="success printButton radius">Add to Cart</td>
    </tr>
{% endfor %}

Can anyone help?

4
  • possible duplicate of How to access the user profile in a Django template? Commented Oct 20, 2012 at 16:09
  • @danihp Not exactly: babbaggeii doesn't know that he has to access the user profile. Commented Oct 20, 2012 at 16:14
  • I've just tried to use {{ request.user.username }} and nothing is returned. Do I need to include anything else? Commented Oct 20, 2012 at 16:20
  • @babbaggeii To use {{ request.anything }}, you need to be using a RequestContext, which you should always be using anyway. Commented Oct 20, 2012 at 16:29

1 Answer 1

2

user_list is a QuerySet of User objects, that don't have the attributes that you are looking for.

You should be using user.get_profile.rating and user.get_profile.reliability.

Indeed, get_profile() will return the profile associated to an User provided you set the setting AUTH_PROFILE_MODULE to your UserProfile type. In a template, you don't need to use (), user.get_profile gives you access to user's profile.

You will also need to ensure that the user actually has a profile in the first place.


Profiles are going away in Django 1.5, so now might not be the best time to start using them!

Sign up to request clarification or add additional context in comments.

2 Comments

Do I have to include anything else? I've just tried {{ user.get_profile.rating }} and {{ user.username }} and they both return blank.
@babbaggeii Are you sure there is anything in your user_list? Can you use {% debug %} to investigate that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.