djangodjango-profiles

Django profile attributes not shown in template


For some reason I can not get custom user profiles to work. The following is what I tried:

models.py:

class UserProfile(models.Model):
    #user = models.ForeignKey(User, unique=True)
    user = models.OneToOneField(User)
    program = models.ForeignKey("Program")
    book_date = models.DateField('book date')
    def __unicode__(self):
            return "%s" % (self.user)
#User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

views.py:

def showProgram(request,program_id):
    program = Program.objects.get(id=program_id)
    t = loader.get_template('controltool2/showProgram.html')
    c = Context({'program':program,})
    return HttpResponse(t.render(c))

showProgram.html:

<h1>Your Program: {{ request.user.get_profile.program }}</h1>

I also tried with

{{ user.get_profile.program }}

or

{{ request.user.get_profile().program }}

or using the lambda function (see #comment in models.py)

Am I missing something? Thanks for any hint!

Ah yes, and I did

AUTH_PROFILE_MODULE = 'mymodelname.UserProfile'

Solution

  • You should use RequestContext in place of Context:

    from django.template import RequestContext
    ###
    ###
    c = RequestContext(request, {'program': program})
    

    Also, you must have django.core.context_processors.request in TEMPLATE_CONTEXT_PROCESSORS.