django可重复使用的重定向最佳实践

时间:2013-03-21 00:56:14

标签: django redirect

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

这是我的代码:

# 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))

我想我可以在视图函数中检查返回类型,但这看起来有点混乱。

1 个答案:

答案 0 :(得分: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
            })