0

I am facing one weird issue while using Django. I can see two entry of the video I am submitting in the database as the page where I am submitting the form refreshes automatically after submission (that is okay to refresh, as I can see the updated results in the table).

But the problem is while refreshing it resubmits the form. And if I manually refresh the page also it keeps submitting new videos. After doing some research I've found articles which leads to the problem in views.py in the application.

There is a similar question as well but the way they did I am not sure how to integrate with my view as I am returning some args to the page too. (Reference article: django form resubmitted upon refresh)

Below is the code which I already understand less.

# Uploading videos form
    if not request.method == "POST":
        f =  UploadForm()   # Send empty form if not a POST method
        args = {"profile_data": profile_data, "video_data": video_data, "form": f}
        return render(request, "home.html", args)

    f = UploadForm(request.POST, request.FILES) # This line is to upload the actual user's content.
    if not f.is_valid():   # Q: Why do we need to check this? And if we do then is the right place and way to do it?
        args = {"profile_data": profile_data, "video_data": video_data}
        return render(request, "home.html", args)

    process_and_upload_video(request)
    args = {"profile_data": profile_data, "video_data": video_data}
    return render(request, "home.html", args)
disp_name
  • 1,448
  • 2
  • 20
  • 46

1 Answers1

1

This is a known issue when you render a response for a given successful POST request. Normally one uses the Post/Redirect/Get [wiki] pattern here: in case the POST is successful, you redirect to the view, such that the browser makes a new GET request, and thus will not resubmit upon refresh, like:

    # Uploading videos form
    if not request.method == "POST":
        f =  UploadForm()   # Send empty form if not a POST method
        args = {"profile_data": profile_data, "video_data": video_data, "form": f}
        return render(request, "home.html", args)

    f = UploadForm(request.POST, request.FILES) # This line is to upload the actual user's content.
    if not f.is_valid():   # Q: Why do we need to check this? And if we do then is the right place and way to do it?
        args = {"profile_data": profile_data, "video_data": video_data}
        return render(request, "home.html", args)

    process_and_upload_video(request)
    return redirect('some_view')

Where some_view is usually a listview, or the same view to enable submitting a new entry.

Note that you probably should refactor the above code: you here use a lot of negative logic, which makes it rather complex.

There are also some odd patterns in your code: for example you process the video in the view itself, which is typically not a good idea, since if this takes a lot of time, the request will timeout. Typically one uses asynchronous tasks (for example with RabbitMQ) to do timeconsuming processing, see for example this article.

The form.is_valid() is typically used to check if all required elements are in the request.POST and request.FILES (required fields and files, are these fields valid ones, etc.). A ModelForm adds some extra programmer convenience to convert the request into a model object.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555