'Django Form processing files

So I have the following code:

# the view

class UpdateDateView(LoginRequiredMixin, UpdateView):
    model = Date
    form_class = DateForm
    template_name = 'app/form/date_edit_form.html'

    def save_photos(self, date) -> None:
        photos = self.request.FILES.getlist('photo')
        current_photos = self.request.FILES.getlist('custom-photo-name') # this is not working
        for photo in photos:
            Photo.objects.create(date=date, photo=photo)

    def form_valid(self, form):
        date = form.save()
        self.save_photos(date)
        return super().form_valid(form)
# the form

class DateForm(forms.ModelForm):
    photo = forms.ImageField(required=False)
    
    class Meta:
        model = Date
        exclude = ('user',)
# the Edit form

<form action="{% url 'app:edit-date' date.slug %}" method="post" enctype="multipart/form-data">{% csrf_token %}
      <div class="form-container">

             ...
             <tr>
                  <th><label for="id_photo">Image:</label></th>
                  <td>
                  <input type="file" name="photo" accept="image/*" id="id_photo" multiple>
                  </td>
             </tr>
             <div class="current-photos">
                  {% for photo in date.photos.all %}
                  <div class="photo-wrapper-{{ forloop.counter }}">
                       <img src="{{ photo.photo.url }}" width="200px"><a class="delete-photo" id="{{ forloop.counter }}">Delete</a>
                        <input type="file" name="custom-photo-name" value="{{ photo }}" class="hide" id="photo_{{ forloop.counter }}">
                  </div>
                  {% endfor %}
             </div>
       </div>
       <div class="buttons">
             <input type="submit" value="Save" class="create-button redirection no_decoration">
             <a href="{% url 'app:date_details' date.slug %}" class="redirection no_decoration">Back</a>
       </div>
</form>
# js (jquery)

$('.delete-photo').on('click', function() {
  const id = $(this).attr('id');
  const div_class = `.photo-wrapper-${id}`;
  $(div_class).remove()
});

I have a CreateView and UpdateView. The ImageField is not required, it is optional. Let's assume I have created a new date with photos. Then I wanted to edit its pictures (delete some and add some new). When I click on tag (Delete), the div wrapper for that photo is being removed. When I try to save my edits, I want to access 2 different lists with photos (ones which were added in the past, and the new photos). This self.request.FILES.getlist('custom-photo-name') seems to do nothing with current photos. Please help, maybe my code logic is bad in general? What I am missing here? What html form looks for when I submit the form, whether for the tag or maybe name attribute? Huge thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source