Django多形式1视图..形式错误问题

时间:2022-10-24 13:28:24

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.

我在一个视图中显示表单错误有些麻烦。我现在是一名学生,已经参加了2个学期的python课程。我决定自己学习一些Django。

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!

我遇到的问题是我试图进行多步骤过程,以便用户可以发布一本书。但是,我想在同一个网址中做到这一点。因此,当用户完成第一个表单并单击提交时,它将继续下一个表单,但URL不会更改。我已经弄明白了。我的问题是,当我到达第二个表单时,已经显示的表单错误与表单一起出现。昨天我花了3个小时试图弄明白,没有运气。所以,如果有人能伸出援助之手,那就太好了!谢谢!

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)) 

1 个解决方案

#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.

您没有任何显示空格式的情况。将request.POST传递给表单并调用is_valid()将导致验证运行,这可能不是您在用户POSTS formone时的意图。正如@karthikr建议的那样,您需要找出一种方法来告诉您的步骤,以便您可以验证并显示相应的表单。

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.

最简单的方法可能是在渲染book_post.html时添加类似args ['step'] = 1的内容,在渲染book_post_form2.html时添加args ['step'] = 2。然后使用隐藏的表单字段在POST上再次将该变量传回服务器,并检查它以确定需要验证的表单。

#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.

您没有任何显示空格式的情况。将request.POST传递给表单并调用is_valid()将导致验证运行,这可能不是您在用户POSTS formone时的意图。正如@karthikr建议的那样,您需要找出一种方法来告诉您的步骤,以便您可以验证并显示相应的表单。

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.

最简单的方法可能是在渲染book_post.html时添加类似args ['step'] = 1的内容,在渲染book_post_form2.html时添加args ['step'] = 2。然后使用隐藏的表单字段在POST上再次将该变量传回服务器,并检查它以确定需要验证的表单。