我是django和Google App Engine的新手,我在使用数据存储时遇到了麻烦。每次我进行查询,例如
db.GqlQuery("SELECT * FROM Listing ORDER BY date DESC LIMIT 10")
我收到错误:
'WSGIRequest' object has no attribute 'user'
这个错误似乎是在django核心的context_processors.py中生成的。现在,我在互联网上发现的建议是评论用户相关的INSTALLED_APPS和MIDDLEWARE_CLASSES,但这似乎没有帮助。我的代码如下所示:
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',
)
我的列表对象定义如下(它之前有一个作者属性,但现在已注释掉,并且使用新名称重新定义了对象):
class Listing(db.Model):
#author = db.UserProperty()
address = db.StringProperty()
date = db.DateTimeProperty(auto_now_add=True)
coords = db.GeoPtProperty()
有谁知道导致此错误的原因,以及如何解决?是否可能需要以某种方式重置设置?
答案 0 :(得分:1)
<强>更新强>
sdolan建议的解决方案似乎是将以下内容添加到app的settings.py中:
TEMPLATE_CONTEXT_PROCESSORS =( “django.core.context_processors.debug” “django.core.context_processors.i18n”)
这有效删除第三个默认处理器django.core.context_processors.auth(不应该存在,因为对于AppEngine我们不需要Django的auth组件)。
谢谢你,sdolan,解决方案!希望其他人也可以使用它。 :)
@Nick ,我认为值得在教程中提出关于CONTEXT_PROCESSORS的这篇黄金文章(http://code.google.com/appengine/articles/django.html)
(问题的原始跟进)
遇到同样的问题,寻找解决方案....当settings.py包含
时,一切正常DEBUG = True
但是当我切换到
时,会弹出这个错误(并且会影响我继续学习的动力)DEBUG = False
<@> @Nick Johnson,这是堆栈跟踪: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'