Django.fun

Execute a Django view and redirect to another view like a request.POST

When I access my web page, let's say example.com, that url is associated with my index view.

def index(request, pid):
    context = {}
    context['nbar'] = 'index'
    if request.method == 'POST':
        User = get_user_model()
        users = User.objects.all()
        context['users'] = users
        context['project_values'] = Project.objects.get(id=pid)
    return render(request, 'index.html', context)

Inside my template (index.html) I have a button that simply changes the state of a certain project Active <-> Inactive

If the user clicks the button, I want the view activate_project or deactivate_project to run in background and when the execution is finished, just reload index.html as a post.REQUEST .

What I've tried: Activate - Deactivate views:

def activate_project(request, pk):
    if request.method == 'POST':
        project_to_activate = ITT.objects.get(pk=pk)
        if project_to_activate.status == "Inactive":
            project_to_activate.status = "Active"
            project_to_activate.save()
            return redirect('/index/'+str(project_to_activate.id)+'/')

def deactivate_project(request, pk):
    if request.method == 'POST':
        project_to_deactivate = ITT.objects.get(pk=pk)
        if project_to_deactivate.status == "Active":
            project_to_deactivate.status = "Inactive"
            project_to_deactivate.save()
            return redirect('/index/'+str(project_to_deactivate.id)+'/')

The problem comes here: return redirect('/index/'+str(project_to_deactivate.id)+'/')

If I use the redirect, it is treated as a GET instead of a POST.

<WSGIRequest: GET '/index/5/'>

In short, I need index to know that I am coming from deactivate_project or activate_project, how can I make the view index smart enough to know that?

Answers: 0