chirag7jain
chirag7jain

Reputation: 1547

Getting error on redirect django

What am I trying to do is

if a user comes with a slug

Case 1

'new' I am either creating a new box for him or fetching his existing empty box and redirecting him based on the slug of the box

Case 2

anything else works fine

URL pattern is

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

I am getting an error

dict' object has no attribute 'has_header'

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}

Stack Trace

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'

Upvotes: 1

Views: 457

Answers (1)

Syed Suhail Ahmed
Syed Suhail Ahmed

Reputation: 695

In the else condition of the get method, you are trying to return an object instead of a response. You have to return a response like return render(request, template_name, {'box': box, 'drafts': box.drafts}) instead of return {'box': box, 'drafts': box.drafts} .

Upvotes: 2

Related Questions