'WSGIRequest' object has no attribute 'user' in Google App Engine
I'm new to django and Google App Engine, and I'm having trouble with using the datastore. Every time I make a query, such as
db.GqlQuery("SELECT * FROM Listing ORDER BY date DESC LIMIT 10")
I receive the error:
'WSGIRequest' object has no attribut开发者_如何学Goe 'user'
This error seems to be generated in context_processors.py within the django core. Now, the advice I've found on the Internet said to comment out user-related INSTALLED_APPS and MIDDLEWARE_CLASSES, but this does not seem to help. My code looks like this:
MIDDLEWARE_CLASSES = (
# 'django.middleware.common.CommonMiddleware',
# 'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.contrib.auth.middleware.AuthenticationMiddleware',
# 'django.middleware.doc.XViewMiddleware',
)
INSTALLED_APPS = (
# 'django.contrib.auth',
'django.contrib.contenttypes',
# 'django.contrib.sessions',
'django.contrib.sites',
)
My Listing object is defined as the following (it had a author property earlier, but this is now commented out and the object was redefined with a new name):
class Listing(db.Model):
#author = db.UserProperty()
address = db.StringProperty()
date = db.DateTimeProperty(auto_now_add=True)
coords = db.GeoPtProperty()
Does anyone know what is causing this error, and how to fix it? Is it perhaps a case of having to reset the settings somehow?
UPDATE
The solution suggested by sdolan seems to be to add the following to the settings.py of the app:
TEMPLATE_CONTEXT_PROCESSORS = ( "django.core.context_processors.debug", "django.core.context_processors.i18n")
This effectively removes the third default processor, django.core.context_processors.auth (which shouldn't be there because for AppEngine we don't want Django's auth component).
Thank you, sdolan, for the solution! hopefully someone else can use it, too. :)
@Nick, I think it's worth putting this golden piece about CONTEXT_PROCESSORS in the tutorial (http://code.google.com/appengine/articles/django.html)
(Original followup to the question)
Have the same problem, looking for solution.... All works fine when settings.py contains
DEBUG = True
but this error pops up (and kills my motivation to proceed with learning) when I switch to
DEBUG = False
@Nick Johnson, here's the stack trace:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3211, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3154, in _Dispatch
base_env_dict=env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 527, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2404, in Dispatch
self._module_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2314, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2212, in ExecuteOrImportScript
script_module.main()
File "C:\Dev\appengine\djangotest\main.py", line 37, in main
util.run_wsgi_app(application)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\util.py", line 97, in run_wsgi_app
run_bare_wsgi_app(add_wsgi_middleware(application))
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\util.py", line 115, in run_bare_wsgi_app
result = application(env, _start_response)
File "C:\Program Files (x86)\Google\google_appengine\lib\django\django\core\handlers\wsgi.py", line 189, in __call__
response = self.get_response(request)
File "C:\Program Files (x86)\Google\google_appengine\lib\django\django\core\handlers\base.py", line 103, in get_response
return callback(request, **param_dict)
File "C:\Program Files (x86)\Google\google_appengine\lib\django\django\views\defaults.py", line 79, in page_not_found
return http.HttpResponseNotFound(t.render(RequestContext(request, {'request_path': request.path})))
File "C:\Program Files (x86)\Google\google_appengine\lib\django\django\template\context.py", line 100, in __init__
self.update(processor(request))
File "C:\Program Files (x86)\Google\google_appengine\lib\django\django\core\context_processors.py", line 18, in auth
'user': request.user,
AttributeError: 'WSGIRequest' object has no attribute 'user'
精彩评论