Search code examples
djangotemplatesdjango-templatesprofilesdjango-profiles

Django {{ profile }} globally available in templates


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.


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.