Code

Opened 7 years ago

Closed 7 years ago

#4049 closed (fixed)

`auth` context processor shouldn't error if `AuthenticationMiddleware` isn't being used

Reported by: David S. <dschein@…> Owned by: gregorth
Component: Core (Other) Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

TEMPLATE_CONTEXT_PROCESSORS default includes django.core.context_processors.auth. This callable assumes that something (presumably django.contrib.auth.middleware.AuthenticationMiddleware) has added an attribute, 'user', to the request. So django.core.context_processors.auth throws an AttributeError exception.

It seems this should be better documented or maybe just wrap the return in the auth method with a try and return {} if there is an AttributeException. Anyway, most everyone must use auth!

Attachments (1)

AuthenticationMiddleware.diff (910 bytes) - added by gregorth 7 years ago.
patch against revision 6213

Download all attachments as: .zip

Change History (7)

comment:1 Changed 7 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from django.core.context_processors.auth should be in contrib to `auth` context processor shouldn't error if `AuthenticationMiddleware` isn't being used
  • Triage Stage changed from Unreviewed to Accepted

It seems there are two facets to this ticket:

  1. django.core.context_processors.auth should be in contrib
  2. context processor auth fails if AuthenticationMiddleware isn't being used

They seem two independent issues, so I'm going to rename the title of this one to issue 2 which seems the more pressing. Thanks for the report David, and feel free to open another ticket just focussing on moving the processor to auth (which would be a hard sell with it breaking backwards compatibility with every default project ever created)

comment:2 Changed 7 years ago by Collin Grady <cgrady@…>

Projects created from manage.py startproject do not define TEMPLATE_CONTEXT_PROCESSORS though, so they're all using the default value from global_settings.py.

If the processor was moved and the default TEMPLATE_CONTEXT_PROCESSORS value adjusted, they'd all work just fine - only those who redefined the value will have to update their settings.

comment:3 Changed 7 years ago by gregorth

  • Owner changed from nobody to gregorth
  • Status changed from new to assigned

Changed 7 years ago by gregorth

patch against revision 6213

comment:4 Changed 7 years ago by gregorth

  • Has patch set

comment:5 Changed 7 years ago by gregorth

  • Triage Stage changed from Accepted to Ready for checkin

comment:6 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [6356]) Fixed #4049 -- Improved error handling in auth() context processor. Based on a patch from gregorth.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.