Django None类型对象没有属性status

5 投票
4 回答
6411 浏览
提问于 2025-04-16 07:19

我在使用Django时遇到了以下错误:

NoneType对象没有status_code这个属性

以下是日志中的输出内容:

Environment:

Request Method: GET
Request URL: http://192.168.2.206:8080/institutes_admin/
Django Version: 1.1.1
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
 'django.contrib.admin',
 'django.contrib.contenttypes',
 'django.contrib.markup',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.comments',
 'mysite.registration',
 'mysite.profiles',
 'mysite.epw',
 'mysite.remember_me',
 'mysite.avatar',
 'mysite.django_documents',
 'mysite.inlines',
 'mysite.blog',
 'mysite.forum',
 'tagging']
Installed Middleware:
('django.middleware.cache.UpdateCacheMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.cache.FetchFromCacheMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'mysite.remember_me.views.AutoLogout')


Traceback:
File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/pymodules/python2.6/django/utils/decorators.py" in _wrapped_view
  56.                 result = middleware.process_response(request, response)
File "/usr/lib/pymodules/python2.6/django/middleware/cache.py" in process_response
  80.         if not response.status_code == 200:

Exception Type: AttributeError at /institutes_admin/
Exception Value: 'NoneType' object has no attribute 'status_code'

4 个回答

0

我在尝试查看我的 /history/ 页面时遇到了类似的错误,这个页面的数据来自日志条目和所有历史对象。

另外,我还从 Excel 表格解析数据到 REST API 框架。有一次在解析 Excel 表格时,我在管理员页面查看时丢失了一些对象(数据)。于是我把 PostgreSQL 数据库删除了,重新创建了数据库,还创建了超级用户的一些设置。

然后我又把解析文件中的数据导入到 REST API。这次一切都正常了。

我可能在你的情况中完全错了,但这个方法对我解决下面的错误有效。

    AttributeError at /history/

'NoneType' object has no attribute 'pk'

Request Method:     GET
Request URL:    http://127.0.0.1:8000/history/
Django Version:     1.7.2
Exception Type:     AttributeError
Exception Value:    

'NoneType' object has no attribute 'pk'

Exception Location:     /home/praneeth/wru-pam/pam_site/rev_history/views.py in history_list, line 12
Python Executable:  /home/praneeth/wru-pam/venv/bin/python
Python Version:     3.4.0
Python Path:    

['/home/praneeth/wru-pam/pam_site',
 '/home/praneeth/wru-pam/venv/src/django-reversion-compare',
 '/home/praneeth/wru-pam/venv/lib/python3.4',
 '/home/praneeth/wru-pam/venv/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/praneeth/wru-pam/venv/lib/python3.4/lib-dynload',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/praneeth/wru-pam/venv/lib/python3.4/site-packages']

Server time:    Wed, 18 Mar 2015 14:22:32 +0000

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/history/

Django Version: 1.7.2
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'corsheaders',
 'pam',
 'reversion',
 'djcelery',
 'rev_history',
 'reversion_compare')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'reversion.middleware.RevisionMiddleware',
 'simple_history.middleware.HistoryRequestMiddleware')


Traceback:
File "/home/praneeth/wru-pam/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/praneeth/wru-pam/pam_site/rev_history/views.py" in history_list
  12.         version_list = Version.objects.filter(object_id=i.object.pk)

Exception Type: AttributeError at /history/
Exception Value: 'NoneType' object has no attribute 'pk'
1

你在某个地方丢失了你的响应对象。

如果autologout有一个叫process_response的方法,我建议你去看看那里。如果你把autologout的代码和视图的代码一起贴出来,可能很快就能找到问题所在。

4

处理 institutes_admin 这个网址的视图没有返回任何响应,所以当中间件试图缓存这个响应时就出错了。你需要把那个视图的代码发上来——请直接在这里发,不要放在其他网站上。

撰写回答