重定向django时出错

时间:2016-09-01 10:25:34

标签: python django django-1.7

我想做的是

如果用户带有slu

案例1

'新'我要么为他创建一个新盒子,要么取出他现有的空盒子并根据盒子的长度重定向他

案例2

其他一切正常

网址格式

url(r'^box/manage/(?P<slug>.*)/$', login_required(views.ManageBoxView.as_view()), name='manage_box'),

我收到错误

  

的dict&#39;对象没有属性&#39; has_header&#39;

class ManageBoxView(TemplateView):
  template_name = "art/manage_box.html"

  def get(self, request, **kwargs):
    if kwargs['slug'] == 'new':
      box = Box.get_or_create_empty_box(self.request.user)
      return redirect('manage_box', slug= box.slug)
    else:
      box = Box.objects.get(slug=kwargs['slug'])
      return {'box': box, 'drafts': box.drafts}

堆栈跟踪

Environment:

Request Method: GET
Request URL: http://localhost:8000/box/manage/10-untitled-admin/

Django Version: 1.7.4
Python Version: 2.7.11
Installed Applications:
('django_admin_bootstrapped',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.messages',
 'django.contrib.sites',
 'django.contrib.humanize',
 'django.contrib.staticfiles',
 'gunicorn',
 'rest_framework',
 'imagekit',
 'utils',
 'users',
 'arts',
 'notification',
 'connect',
 'payment',
 'products',
 'orders',
 'social')
Installed Middleware:
('sslify.middleware.SSLifyMiddleware',
 'django.middleware.gzip.GZipMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'raygun4py.middleware.django.Provider')


Traceback:
File "/home/cj/.vrtualenvs/canvs/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  204.                 response = middleware_method(request, response)
File "/home/cj/.vrtualenvs/canvs/local/lib/python2.7/site-packages/django/contrib/sessions/middleware.py" in process_response
  30.                 patch_vary_headers(response, ('Cookie',))
File "/home/cj/.vrtualenvs/canvs/local/lib/python2.7/site-packages/django/utils/cache.py" in patch_vary_headers
  148.     if response.has_header('Vary'):

Exception Type: AttributeError at /box/manage/10-untitled-admin/
Exception Value: 'dict' object has no attribute 'has_header'

1 个答案:

答案 0 :(得分:2)

在get方法的else条件中,您尝试返回对象而不是响应。您必须返回return render(request, template_name, {'box': box, 'drafts': box.drafts})而不是return {'box': box, 'drafts': box.drafts}的回复。