[Answered ]-Double post after refresh django

1👍

You should implement the Post/Redirect/Get architectural pattern [wiki], and thus return a redirect such that the browser will make a GET request next, and thus prevent making another POST request when refreshing the page:

def product_detail(request, product_id):
    product = get_object_or_404(Product, pk=product_id)
    if request.method == 'POST':
        rating = request.POST.get('rating', 3)
        content = request.POST.get('content', '')
        Review.objects.create(
            product=product,
            rating=rating,
            content=content,
            created_by=request.user
        )
        # redirect to the same page 🖟
        return redirect('product_detail', product_id=product_id)

    reviews = Review.objects.filter(product=product)
    context = {
        'product': product,
        'reviews': reviews
    }
    return render(request, 'products/product_detail.html', context)

Leave a comment