2

Creating simple app using GAE / Django-nonrel (I don't think the problem is specific to GAE or the nonrel fork, most likey PEBKAC as python/django noob and would occur on basic django installation).

I am using django.contrib.auth for authentication.

In settings.py

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware', )

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

I've created superuser with manage.py

I've got following in templates base.html which is used in others by {% extends 'base.html' %}

{% if user.is_authenticated %} 
    Hello {{ user.username }} 
    [<a href="{% url django.contrib.auth.views.logout %}">sign out</a>] 
{% else %} 
    [<a href="{% url django.contrib.auth.views.login %}">sign in</a>] 
{% endif %} 

And in urls.py the standard authentication stuff (from django.contrib.auth.forms import AuthenticationForm etc).

Problem is that I can authenticate successfully, username/password checking is working (can't use incorrect user/pwd) and I am authenticated on the admin pages - but not on the other pages - or rather I am but user is null (None).

I think that "django.contrib.auth.context_processors.auth" is the magic that makes this happen but that is setup in settings.py as shown above.

Any tips on how to track this problem down?

EDIT (expanding on Daniels answer as as can't do code formatting in comments)

in views.py I had :-

def detail(request):
    obj = get_object_or_404(MyModel, pk=some_id)    
    return render_to_response('myapp/index.html', {'MyModel': obj})

Should have been

    return render_to_response('myapp/index.html', {'MyModel': obj}, RequestContent(request))
Ryan
  • 23,871
  • 24
  • 86
  • 132

2 Answers2

4

Are you using a RequestContext to render your template? Context processors aren't applied unless you do.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • doh... Its right there, under "NOTE - Yes you, read this, right here" ;) – Ryan Jan 20 '11 at 22:55
  • 2
    I am having this problem too: after login correctly, I redirect to home page, wich view returns render_to_response('myapp/shop_list.html', {'shop': entry_list,}, context_instance=RequestContext(request)) But, at this point, request.user is an AnonymusUser so when the home page template loads, user.is_authenticated is always false. What am I missing? – juankysmith Mar 06 '12 at 12:08
  • saved me, although the link is not working anymore, see here: https://docs.djangoproject.com/en/1.9/ref/templates/api/#django.template.RequestContext – platzhersh Feb 17 '16 at 16:47
0

See django-postman discards RequestContext . With that you're assigning TEMPLATE_CONTEXT_PROCESSORS 2 items, you're overriding the default items.

Community
  • 1
  • 1
aladagemre
  • 592
  • 5
  • 16