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?

babbaggeii
  • 7,577
  • 20
  • 64
  • 118
  • possible duplicate of [How to access the user profile in a Django template?](http://stackoverflow.com/questions/422140/how-to-access-the-user-profile-in-a-django-template) – dani herrera Oct 20 '12 at 16:09
  • @danihp Not exactly: babbaggeii doesn't know that he has to access the user profile. – Thomas Orozco Oct 20 '12 at 16:14
  • I've just tried to use {{ request.user.username }} and nothing is returned. Do I need to include anything else? – babbaggeii Oct 20 '12 at 16:20
  • @babbaggeii To use `{{ request.anything }}`, you need to be using a [`RequestContext`](https://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext), which you should _always_ be using anyway. – Thomas Orozco Oct 20 '12 at 16:29

1 Answers1

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!

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • Do I have to include anything else? I've just tried {{ user.get_profile.rating }} and {{ user.username }} and they both return blank. – babbaggeii Oct 20 '12 at 16:25
  • @babbaggeii Are you sure there is anything in your `user_list`? Can you use {% debug %} to investigate that? – Thomas Orozco Oct 20 '12 at 16:27