使用2个kwargs重定向时的builtins.ValueError

时间:2016-09-09 14:25:11

标签: django django-views django-urls

我想在我的一个视图中执行重定向,并且我遇到的错误没有太多细节。

我的urls.py:

urlpatterns = [
    url(
            regex=r'^(?P<pk>[0-9]+)/$',
            view=views.SheetDetailView.as_view(),
            name='detail'
        ),
    url(
            regex=r'^(?P<pk>[0-9]+)/branch/(?P<branch_slug>[a-zA-Z0-9-]+)/$',
            view=views.SheetDetailView.as_view(),
            name='detail'
        ),

]

我的观点

class SheetDetailView(LoginRequiredMixin, UserIsLinkedToSheetMixin, UserPassesTestMixin, DetailView):
    model = Sheet
    def get_context_data(self, **kwargs):
        context = super(SheetDetailView, self).get_context_data(**kwargs)
        #if no branch is requested, show the master
        if 'branch_slug' in self.kwargs:
            branch = self.get_object().get_branch(self.kwargs['branch_slug'])
            context['branch']=branch
            return context
        else:
            # redirect to master                    
            sheet_id = self.get_object().pk
            branch_slug = self.get_object().get_master().slug                    
            return redirect(reverse('sheets:detail', kwargs={'pk':sheet_id, 'branch_slug':branch_slug}), permanent=False)

我的错误信息:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 177, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 230, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 289, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/usr/local/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.5/site-packages/six.py", line 686, in reraise
    raise value
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 174, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 172, in get_response
    response = response.render()
  File "/usr/local/lib/python3.5/site-packages/django/template/response.py", line 160, in render
    self.content = self.rendered_content
  File "/usr/local/lib/python3.5/site-packages/django/template/response.py", line 137, in rendered_content
    content = template.render(context, self._request)
  File "/usr/local/lib/python3.5/site-packages/django/template/backends/django.py", line 92, in render
    context = make_context(context, request)
  File "/usr/local/lib/python3.5/site-packages/django/template/context.py", line 291, in make_context
    context.push(original_context)
  File "/usr/local/lib/python3.5/site-packages/django/template/context.py", line 61, in push
    return ContextDict(self, *dicts, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/template/context.py", line 20, in __init__
    super(ContextDict, self).__init__(*args, **kwargs)
ValueError: dictionary update sequence element #0 has length 0; 2 is required

当我打印出reverse()结果并将其粘贴到我的浏览器中时,一切正常。

1 个答案:

答案 0 :(得分:0)

您正尝试从get_context_data返回重定向。但是,正如名称所暗示的那样,该方法应该返回渲染模板的上下文。您可能需要将该特定逻辑放在getdispatch中。