Question

I'm building app with django-registration and django-profiles. Everywhere on the page I have the section displaying data of currently logged in user (like first and last name, using syntax like: {{ user.first_name }}) and it works fine. It is done with subpages templates extending the one main template with HTML structure and the section mentioned.

Now I try to add the user image (using {{ profile.image }}) to the section and there is a problem with availability of {{ profile }} template variable everywhere but on the following the pages:

In settings.py I have:

AUTH_PROFILE_MODULE = 'intranet.UserProfile'

TEMPLATE_CONTEXT_PROCESSORS = (
   'django.core.context_processors.static',
)

Adding "django.contrib.auth.context_processors.auth", to TEMPLATE_CONTEXT_PROCESSORS does not change anything.

My UserProfile class in models.py is:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    image = models.ImageField(upload_to=user_image_name,
        blank=True, null=True, verbose_name='User photo')

urls.py:

(r'^profiles/edit/', 'profiles.views.edit_profile', {'form_class': ProfileForm, }),
(r'^profiles/', include('profiles.urls')),

so the rest is set by default in django-profiles urls.py file.

I would like to be able to use {{ profile }} template variable everywhere (not only on the profile pages) in the templates of the application to make it possible to use it in the main template that is extended by the others.

Please let me know how can I get this. Any help will be very appreciated.

I'm using Django in version 1.3.1, django-registration in version 0.7 and django-profiles in version 0.2.

Was it helpful?

Solution

I think you want user.get_profile().

If you are using RequestContext and have auth in the context processors list in settings.py, then try {{user.get_profile.image}} and see if it does what you want.

OTHER TIPS

I think the above answer by "Peter Rowell" answer must have worked for you in this specific case because of following reasons,

  1. User Object is available by "django.contrib.auth.context_processors.auth" context processor in templates.
  2. As you can use all methods of available Python Objects in django template context User.get_profile() method is available.

But If you need any other Object ( for example, Entry Object with id=2 or if you didn't have "django.contrib.auth.context_processors.auth" ) in your templates you can supply any objects in a dictionary to Template from your view as following

def view_needing_profile(request):
    c_user = request.user
    c_profile = c_user.get_profile()
    c_entry = Entry.objects.get(id=2)

    return render(request, 'template_using_profile_and_entry.html',
                           {'profile' : c_profile, 'entry' : c_entry } )                                    

You could also put something like this in your userprofile app models.py file

# Fetches the user profile if it exists, otherwise, it creates one User.get_or_create_profile = property(lambda u: Profile.objects.get_or_create(user=u)[0])

This ensures you will always return a profile object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top