1

im having some trouble with displaying form errors within one view. I'm currently a student and have been taking python classes for 2 semesters. I decided to learn some Django on my own.

The issue that i have come across is that im trying to have a multistep process so a user can post a book. But, i want to do it within the same url. so when the user finishes the first form and clicks submit it will go on to the next form but the url wont change. I have figured this out. my issues is that when i get to the second form there are form errors already displaying that appear along with the form. I spend like 3 hours yesterday trying to figure out and no luck. so if anyone can give a helping hand that would be great! thanks!

def book_post(request):
    if request.method == 'POST':
        formone = bookFormOne(request.POST)        
        formtwo = bookFormTwo(request.POST, request.FILES)
        if formtwo.is_valid():   
            #.....do form2 valid stuff
            return HttpResponseRedirect('/success')        
        if formone.is_valid() :
            #....do form1 valid stuff
            #formtwo = bookFormTwo()..if i add this the errors wont display but then    errors from the first form spill over and it wont allow the second form to be valid...###

            args = {'form2':formtwo,'isbn':isbn,'subject':subject}
            args.update(csrf(request)) 
            return render_to_response('book_post_form2.html', args,context_instance=RequestContext(request))        

        else:
            args = {}
            args['form'] = formone
            args['form2'] = formtwo
            args.update(csrf(request))            


    else: 
        form = bookFormOne()
        args = {'form':form}
        args.update(csrf(request)) 
    return render_to_response('book_post.html', args,context_instance=RequestContext(request)) 
3
  • this is not very clean logic. Have a step_number as a context variable, and validate forms based on that. That way, you can validate the correct step without any issues Commented Jun 11, 2013 at 17:47
  • @karthikr how can i get this done? do you mind explaining a little more please. Thank You! Commented Jun 11, 2013 at 18:01
  • I guess Shane is kind of on the same lines of what i would have suggested. Commented Jun 11, 2013 at 18:59

1 Answer 1

1

You don't have any case in which you're displaying an empty formtwo. Passing request.POST to the form and calling is_valid() is going to cause validation to run, which presumably isn't what you intend when the user POSTS formone. As @karthikr suggests, you need to figure out a way to tell what step you're so you can validate and display the appropriate form.

The easiest way might be to add something like args['step'] = 1 when rendering book_post.html and args['step'] = 2 when rendering book_post_form2.html. Then pass that variable back to the server again on POST using a hidden form field, and check it to decide which form you need to validate.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the help! i got it to work nicely now!! thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.