"使用协议'内容类型'"不安全地重定向到网址Django视图中的错误

时间:2015-08-11 20:14:13

标签: django django-class-based-views

我在get_success_url()课程中使用CreateView时出现此问题:

def get_success_url(self):
    search = searchViews.getIndexInstance()
    search.mainfunc(self.contentForSearch, self.discussion.pk)
    return HttpResponseRedirect(reverse('discussion-detail', kwargs={'slug':self.discussion.slug}))

这里是完整的错误堆栈跟踪:

Traceback:
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/nithin/djangopro/viewanalyse/discussion/views.py" in dispatch
  30.         return super(LoggedInMixin, self).dispatch(*args, **kwargs)
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  89.         return handler(request, *args, **kwargs)
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post
  249.         return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post
  215.             return self.form_valid(form)
File "/home/nithin/djangopro/viewanalyse/discussion/views.py" in form_valid
  374.         return super(DiscussionContentUpdateCreateView, self).form_valid(form)
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/views/generic/edit.py" in form_valid
  194.         return super(ModelFormMixin, self).form_valid(form)
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/views/generic/edit.py" in form_valid
  108.         return HttpResponseRedirect(self.get_success_url())
File "/home/nithin/djangopro/djangoenv/local/lib/python2.7/site-packages/django/http/response.py" in __init__
  456.             raise DisallowedRedirect("Unsafe redirect to URL with protocol '%s'" % parsed.scheme)

Exception Type: DisallowedRedirect at /discussion/discussion-content-update/why-i-believe-in-reincarnation-g-hijo-2015-08-09-162236316999/
Exception Value: Unsafe redirect to URL with protocol 'content-type'

原因是什么以及如何解决这个问题?

1 个答案:

答案 0 :(得分:7)

get_success_url方法应该返回网址,而不是HTTP响应。

def get_success_url(self):
    search = searchViews.getIndexInstance()
    search.mainfunc(self.contentForSearch, self.discussion.pk)
    return reverse('discussion-detail',kwargs={'slug':self.discussion.slug})

目前尚不清楚前两行是什么,它们似乎不会被reverse()电话使用。