您好,我正在制作一个简单的表格。表单提交正常,但如果我刷新页面,它会重新提交数据。似乎表单在提交后保存了数据,我假设在提交后请求方法是发布的。问题是提交后清除 Django 表单的最佳方法是什么。提交后,表单变量不应再保留这些值。谢谢
def testimonials(request, template_name="testimonials.html"):
reviews = Reviews.objects.all()
if request.method == 'POST':
form = forms.ReviewsForm(data = request.POST)
# create a new item
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
if form.is_valid():
nameIn = form.cleaned_data['name']
reviewIn = form.cleaned_data['review']
newReview = Reviews(name = nameIn, review = reviewIn)
newReview.save()
return render_to_response(template_name, locals(), context_instance=RequestContext(request))
else:
# This the the first page load, display a blank form
form = forms.ReviewsForm()
return render_to_response(template_name, locals(), context_instance=RequestContext(request))