0

我有许多需要相同功能的视图,所以我试图将该逻辑移动到一个单独的函数中(而不是视图函数)。该函数在 GET 或会话中找到一个值并返回一个模型实例重定向到一个新页面(有点像强制登录)。问题是你不能从一个被调用的函数(我知道)重定向。我应该如何处理这种情况?

这是我的代码:

# This is the called function
def getActiveShowOrRedirect(request):
    show_pk = request.GET.get('s', False)
    if not show_pk:
        show_pk = request.session.get('show_pk', False)
        if not show_pk:
            return HttpResponseRedirect('/setup/')
    active_show = Show.objects.get(pk=show_pk)
    return active_show

def overview(request):
    active_show = getActiveShowOrRedirect(request)

    scenes = Scene.objects.filter(show=active_show)
    scenes = sorted(scenes, key=lambda s: s.name) 

    if request.method == 'POST':
        form = SceneForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            desc = form.cleaned_data['description']
            scene = Scene(name=name.lower(), show=active_show, description=desc, creator=request.user)
            scene.save()

            return HttpResponseRedirect('/overview/')
    else:
        form = SceneForm(initial={'creator':request.user,'show':active_show})

    return render_to_response('vfx_app/overview.html', {'active_show':active_show,'scenes':scenes,'form':form}, context_instance=RequestContext(request))

我想我可以在视图函数中检查返回类型,但这似乎有点乱。

4

1 回答 1

2

装修师怎么样?

def requires_active_show(view):
    "The decorated view must take active show as a second argument."

    def wrapped(request, *args, **kw):
        show_pk = request.GET.get('s') or request.session.get('show_pk')
        if not show_pk:
            return HttpResponseRedirect('/setup/')

        return view(request, Show.objects.get(pk=show_pk), *args, **kw)

    return wrapped

@requires_active_show
def overview(request, active_show):
    scenes = Scene.objects.filter(show=active_show).order_by('name')

    if request.method == 'POST':
        form = SceneForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            desc = form.cleaned_data['description']
            scene = Scene.objects.create(
                         name=name.lower(),
                         show=active_show,
                         description=desc,
                         creator=request.user)

            return HttpResponseRedirect('/overview/')
    else:
        form = SceneForm(initial={'creator': request.user, 'show': active_show})

    return render('vfx_app/overview.html', {
               'active_show': active_show,
               'scenes': scenes,
               'form': form
            })
于 2013-03-21T01:16:05.520 回答