Yousuf Jawwad
Yousuf Jawwad

Reputation: 3097

django multiple choices on form submits

I want to have multiple multiple choices given to user after the form submit in a view.

For example ... Save and Add Another .. or Save and Return to Dashboard.

Is there a way that i can tell my view that if input value is 1, do this .. or do the other thing?

//mouse

Upvotes: 0

Views: 124

Answers (1)

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53971

Of course. Here's a brief (and untested) example of how you can conditionally redirect the user to another page or call a view from within another view and return it

from django.shortcut import render
from django.http import HttpResponseRedirect
def option_one_view(request):
    return render('option_one_template.html', ...)

def option_two_view(request):   
    return render('option_two_template.html', ...)

def main_view(request):
    if request.method == POST:
       form = MyForm(request.POST)
        if form.is_valid():
            if form.cleaned_data['example_field'] == 1:
                return option_one_view(request)
            else if ...:
                return option_two_view(request)
            else:
                return HttpRedirect("http://...")
    ...
    return render('main_template.html', ...)

Upvotes: 2

Related Questions