0

这是我的视图代码

if request.method == 'POST':
        if 'cancel' in request.POST:
            return redirect('incident.views.index')
        if 'password' in request.POST:
      registerform = UserRegisterForm(request.POST)
        registerform = UserRegisterForm(request.POST)
                    createprofileform = UserCreateProfileForm(request.POST)
                    if registerform.is_valid() and createprofileform.is_valid():
                        result = registerform.save(commit=False)
                        result.set_password(request.POST['password'])
                        result.save()
                        member.user_id = user.id
                        member.member_id = result.id
                        member.save()
                        member_profile = UserProfile.objects.get(user=result.id)
                        createprofileform = UserCreateProfileForm(request.POST, instance=member_profile)
                        createprofileform.save()
                        is_member_screen = True
                        member_save_msg = 'New member has been added.'
          return render(request, 'incident/about_me.html',
                               registerform':registerform,
                               'createprofile':createprofileform,
                               'member_save_msg': member_save_msg,
                                  })

每次提交表单后,我想清除已发布的数据,实际上表单正在保存已提交的数据。我不想将页面重定向到其他页面,因为每次保存后都会出现保存成功消息。如何清除已发布每次提交时来自表单的数据。

谢谢

4

1 回答 1

3

重新初始化表单registerformcreateprofileform清空表单 - 当您想要清除数据时,没有 POST 数据。

样本:

if request.method == 'POST':
    if 'cancel' in request.POST:
        return redirect('incident.views.index')
    if 'password' in request.POST:
        registerform = UserRegisterForm(request.POST)
        registerform = UserRegisterForm(request.POST)
        createprofileform = UserCreateProfileForm(request.POST)
        if registerform.is_valid() and createprofileform.is_valid():
           # your code
           ...
           #reset forms
           registerform = UserRegisterForm()
           createprofileform = UserCreateProfileForm()

   return render(request, 'incident/about_me.html',
                           registerform':registerform,
                           'createprofile':createprofileform,
                           'member_save_msg': member_save_msg,
                              })
于 2013-05-24T06:11:56.373 回答