pythondjangodjango-viewsdjango-templatesdjango-file-upload

Infinite POST request on uploading file with Django


I try to upload some files to a server through a web interface with Django.

HTML :

<form method="post" enctype="multipart/form-data" name="upload_file">
    {% csrf_token %}
    <input type="file" name="uploaded_file_list" multiple>
    <button class="rounded-full bg-violet-200 text-violet-700 block p-2" name="upload_file" value="dummy" type="submit">Ajouter des fichiers</button>
</form>

views.py

def media_manager(request):
    file_manager = MediaManager.server.FileManager(django.conf.settings.USER_FILE_ROOT)

    # POST utilisé pour associer les tags aux images
    if request.method == "POST":
        print(request.POST)
        if request.POST.get("upload_file"):
            for uploaded_file in request.FILES.getlist("uploaded_file_list"):
                file_manager.add_file(uploaded_file)

    context_dict = {}
    context_dict["filtered_file_list"]  = MediaManager.filters.ImageFilter(request.GET, queryset=MediaManager.models.UserImage.objects.all())

    return django.shortcuts.render(request, "MediaManager/media_manager.html", context=context_dict)

FileManager.py

def add_file(self, uploaded_file):
    file_system_storage = django.core.files.storage.FileSystemStorage(location=self._user_dir_absolute_path)
    file_system_storage.save(uploaded_file.name, uploaded_file)

FileManager also updates context_dict["filtered_dile_list"] with the files uploaded.

When I upload a file on the browser, the file is correctly uploaded and the web display also correctly add it on the page. I can see the upload POST request.
But this operation is repeated infinitely.

Here is the log (with a request.POST print) :

<QueryDict: {'csrfmiddlewaretoken': ['vCHAoeGg3QVIZDuHAls8lmV7Y8MXHqxGeWQj16N2zJcCUfoML4pVcnsmJGk7R5Er'], 'upload_file': ['dummy']}>
[02/Nov/2022 22:03:23] "POST /media_manager/ HTTP/1.1" 200 19214

1 static file copied to '/home/gautitho/workspace/MonPetitNuage/MonPetitNuage/static', 185 unmodified.

<QueryDict: {'csrfmiddlewaretoken': ['vCHAoeGg3QVIZDuHAls8lmV7Y8MXHqxGeWQj16N2zJcCUfoML4pVcnsmJGk7R5Er'], 'upload_file': ['dummy']}>
[02/Nov/2022 22:03:24] "POST /media_manager/ HTTP/1.1" 200 19580
[02/Nov/2022 22:03:24] "GET /static/MediaManager/user/Couleurs-logo-Overwatch.jpg HTTP/1.1" 200 63919

1 static file copied to '/home/gautitho/workspace/MonPetitNuage/MonPetitNuage/static', 186 unmodified.

<QueryDict: {'csrfmiddlewaretoken': ['vCHAoeGg3QVIZDuHAls8lmV7Y8MXHqxGeWQj16N2zJcCUfoML4pVcnsmJGk7R5Er'], 'upload_file': ['dummy']}>
[02/Nov/2022 22:03:25] "POST /media_manager/ HTTP/1.1" 200 19959
[02/Nov/2022 22:03:25] "GET /static/MediaManager/user/Couleurs-logo-Overwatch.jpg HTTP/1.1" 304 0
[02/Nov/2022 22:03:25] "GET /static/MediaManager/user/Couleurs-logo-Overwatch_tuThoGu.jpg HTTP/1.1" 200 63919

1 static file copied to '/home/gautitho/workspace/MonPetitNuage/MonPetitNuage/static', 187 unmodified.

<QueryDict: {'csrfmiddlewaretoken': ['vCHAoeGg3QVIZDuHAls8lmV7Y8MXHqxGeWQj16N2zJcCUfoML4pVcnsmJGk7R5Er'], 'upload_file': ['dummy']}>
[02/Nov/2022 22:03:26] "POST /media_manager/ HTTP/1.1" 200 20338
[02/Nov/2022 22:03:26] "GET /static/MediaManager/user/Couleurs-logo-Overwatch_tuThoGu.jpg HTTP/1.1" 304 0
[02/Nov/2022 22:03:26] "GET /static/MediaManager/user/Couleurs-logo-Overwatch.jpg HTTP/1.1" 304 0
[02/Nov/2022 22:03:26] "GET /static/MediaManager/user/Couleurs-logo-Overwatch_2i1PCzd.jpg HTTP/1.1" 200 63919

Solution

  • According to docs:

    you should always return an HttpResponseRedirect after successfully dealing with POST data. This tip isn’t specific to Django; it’s good web development practice in general.

    So the view should be like this:

    def media_manager(request):
        file_manager = MediaManager.server.FileManager(django.conf.settings.USER_FILE_ROOT)
    
        # POST utilisé pour associer les tags aux images
        if request.method == "POST":
            print(request.POST)
            if request.POST.get("upload_file"):
                for uploaded_file in request.FILES.getlist("uploaded_file_list"):
                    file_manager.add_file(uploaded_file)
                return redirect("success")
    
        context_dict = {}
        context_dict["filtered_file_list"]  = MediaManager.filters.ImageFilter(request.GET, queryset=MediaManager.models.UserImage.objects.all())
       
    
        return django.shortcuts.render(request, "MediaManager/media_manager.html", context=context_dict)
    
    
    def success(request):
        return django.shortcuts.render(request, "MediaManager/file_success.html")   
    

    Also don't need to give same name in form tag itself as given in button which is upload_file remove that.

    urls.py:

    urlpatterns=[
        #...
        path("file_success/",views.success,name="success")
    ]
        #...
    

    file_success.html:

    <body>
        <h2>The file has been uploaded successfully. </h2>
        <a href="{% url 'your_media_manager_view_name' %}"> uplad page </a> 
    </body>