我有以下视图,允许用户为给定产品添加评论。用户提交表单后,它会将数据添加到数据库中,然后转到 /view_reviews.html。但是,当我刷新页面时,它会再次添加相同的评论。即使用户碰巧刷新页面,如何确保仅添加一次评论?
def add_review(request, product_id):
if request.method == 'POST':
form = ReviewForm(request.POST)
if form.is_valid():
form.save()
product = get_object_or_404(Product, pk=product_id)
reviews = Review.objects.filter(product_id=product_id)
return render(request,'reserve/templates/view_reviews.html',{'form': form, 'product':product, 'reviews':reviews})
else:
form = ReviewForm()
variables = RequestContext(request, {'form': form})
return render_to_response('reserve/templates/create_review.html', variables)