Darek
Darek

Reputation: 35

Django redirect by calling function view?

I need to redirect while keeping the original request. Is this approach inline with Django 3 logic or am I creating some furute catastrofe?

def final_view(request):
    # use some_new_variable to do stuff and then display it in the template
    return render(request, another_template_name, {})

def original_view(request):
    # do stuff
    request.some_new_variable = "x"
    return final_view(request)

Upvotes: 2

Views: 618

Answers (1)

Josh
Josh

Reputation: 285

Not sure exactly what you're trying to accomplish, but you could always pass "some_new_variable" via the URL

For instance:

views.py

def original_view(request):
    x = # do your stuff here
    return redirect('final_view_url', var=x)

def final_view(request, var)
    final_var = # do some additional stuff with var here
    return render(request, another_template_name)

urls.py

path('/final_view/<var>', views.final_view, name="final_view_url")

So you send your original request to "original_view", do some stuff, then pass that variable into the URL to "final_view"

Upvotes: 1

Related Questions