django reusable redirect best practice -


I have several views that require the same functionality, so I want to move that argument to a different function Trying (not a visual function). The function either receives a value in GET or Session, and returns a model example or redirect to a new instance (like forcing a login). The problem is that you can not redirect from a function (which I know) how should I manage this scenario?

This is my code:

  # This function def is 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 (Requested) views = view. Comkfilter (show = Skriy_so) View = sorted (curtains, key = lambda s: Sknam) if request.method == 'POST': form = SceneForm (request.POST) if Formkis_valid (): Name = form.cleaned_data ['name'] desc = form.cleaned_data ['description'] visible = view (name = name.lower (), show = active_show, description = desc, creator = request url) visual sve () returns Actiteepiarpispoint redirect ( '/ overview /') other forms = Sinform (initial = { 'Creator' request. User 'show': Skriy_so}} return Render_to_prpres ( 'Vfx_app / Overviewkhtml', { 'Active_show' Skriy_ I think I can check the return type in the viewing function, I can see the return type in the viewing function, 'curtain': view, 'form': form}, context_instance = requestcon text (request))   

But it seems that something is wrong.

How about a decorator? < Pre> def needed_active_show (see): "Decorated view must be an active show as a second argument." Def wrapped (request, * args, ** kW): show_pk = request.GE T.get ( 's') or request.session.get ( 'show_pk') if not show_pk: Return HttpResponseRedirect ( '/ setup /') See return (request, Show.objects.get (pk = show_pk), * args , ** kw) back wrapped @requires_active_show def Overview (request, active_show): scenario = Scene.objects.filter (show = active_show) .order_by ( 'name') that request.method == 'POST': form = SceneForm ( Request.POST) if form.is_valid (): name = form.cleaned_data ['name'] desc = form.cleaned_data ['description'] view = HttpResponseRedirect ('/ overview /') Other: form = SceneForm (initial = { 'Manufacturer': view = active_show, description = description, manufacturer = request.user). Request.user, 'show': active_show} Return Render ('vfx_app / overview.html', '' active_show '': active_show, 'scenes': scenes, 'form': form}) < / Div>

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -