这是我的观点代码
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,
})
在每个表单提交后,我想清除发布的数据,实际上表单正在保存提交的数据。我不想将页面重定向到其他页面,因为每次保存后都会显示保存成功消息。如何在每次提交时清除表单中的发布数据。
由于
答案 0 :(得分:3)
如果要清除数据,请重新初始化表单registerform
和createprofileform
以清空表单 - 无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,
})