我使用Django 1.11.3并尝试重新定向用户" Save"管理员在某些模型上按下按钮。所以,在我的admin.py
文件中,我必须遵循代码(部分代码):
from django.contrib import admin
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.http import HttpResponseRedirect
from .models import Info
class InfoAdmin(admin.ModelAdmin):
def response_change(self, request, object, form_url=''):
if(object.type == 1):
return HttpResponseRedirect('/admin/pict/data/add/?info=' + str(object.info))
else:
return HttpResponseRedirect('/admin/pict/article/')
admin.site.register(Info, InfoAdmin)
但按下按钮后出现以下错误:
AttributeError at /admin/pict/info/58/change/
'NoneType' object has no attribute 'has_header'
我也尝试使用redirect
和HttpResponse
,但错误仍然存在。
完整追溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/pict/info/58/change/
Django Version: 1.11.3
Python Version: 3.5.2
Installed Applications:
['pict.apps.PictConfig',
'suit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'massadmin']
Installed Middleware:
['whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Users/mick/.virtualenvs/gbs/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Users/mick/.virtualenvs/gbs/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/mick/.virtualenvs/gbs/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/mick/.virtualenvs/gbs/lib/python3.5/site-packages/django/contrib/admin/options.py" in wrapper
551. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/mick/.virtualenvs/gbs/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/Users/mick/.virtualenvs/gbs/lib/python3.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
58. add_never_cache_headers(response)
File "/Users/mick/.virtualenvs/gbs/lib/python3.5/site-packages/django/utils/cache.py" in add_never_cache_headers
274. patch_response_headers(response, cache_timeout=-1)
File "/Users/mick/.virtualenvs/gbs/lib/python3.5/site-packages/django/utils/cache.py" in patch_response_headers
265. if not response.has_header('Expires'):
Exception Type: AttributeError at /admin/pict/info/58/change/
Exception Value: 'NoneType' object has no attribute 'has_header'
我做错了什么?
答案 0 :(得分:0)
response_change
的方法签名不正确。尝试将方法更改为:
def response_change(self, request, obj):
if(obj.type == 1):
return HttpResponseRedirect('/admin/pict/data/add/?info=' + str(obj.info))
else:
return HttpResponseRedirect('/admin/pict/article/')