我有很多意见需要相同的功能,所以我试图将该逻辑移入一个单独的功能(而不是视图功能)。该函数在GET或会话中查找值,并返回模型实例或重定向到新页面(类似于强制登录)。问题是你不能从一个被调用的函数重定向(我知道)。我应该如何处理这种情况?django可重用重定向最佳做法
这里是我的代码:
# 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))
我想我可以为您在视图函数的返回类型,但似乎有点凌乱。
你看过基于类的视图吗?这对他们来说似乎是一个完美的场景 – miki725 2013-03-21 01:12:44
尝试相反的功能 – catherine 2013-03-21 01:19:54