straight55b / app-engine-patch

Automatically exported from code.google.com/p/app-engine-patch
0 stars 0 forks source link

user.is_authenticated #204

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Question...

The {% if user.is_authenticated %} works fine within all the pages for
registration, which load through base.html

My question is... How can I use this within other pages on the site?  I
would like the Welcome/Login to appear on all the pages, but
user.is_authenticated is not accessed there.

Is there a decorator to be added to authenticate users/visitors on other
pages or views?

Thank you.

Original issue reported on code.google.com by ken...@gmail.com on 12 Aug 2009 at 4:14

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
SOLUTION:

Django's render_to_response and template.RequestContext handle this for you.

First, make sure you enable the template auth in settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth',
    'django.core.context_processors.media',
    'django.core.context_processors.request',
    'django.core.context_processors.i18n',
)

Then, in your views.py, you need:

from django.shortcuts import render_to_response 
from django.template import RequestContext

.....

return render_to_response('my_template.html',
                           my_data_dictionary,
                           context_instance=RequestContext(request) )

# By adding this 3rd optional parameter to render_to_response, every 
RequestContext
will contain 'user' (An auth.User instance representing the currently logged-in 
user
(or an AnonymousUser instance, if the client isn't logged in).)

More at:

http://docs.djangoproject.com/en/dev/ref/templates/api/

Original comment by ken...@gmail.com on 16 Aug 2009 at 2:39