0

In my Django project, I have this view:

def create_format(request, **kwargs):
    if request.method == 'POST':
        form = FormatUpdateForm(request.POST)
        if form.is_valid():
            business_type = int(request.POST.get('business_type'))

            ... more fields ...

            constitutive_act_copy = clean_str(request.POST.get('constitutive_act_copy'))
            return HttpResponseRedirect(reverse('formats:formats'))
    elif request.method == 'GET':
        form = FormatUpdateForm()
    return render(request, 'format_form.html', context={'form': form, 'autocomplete': SearchableData.objects.all()})

I have many fields, some of which are FileFields. I want to create an object with the data obtained from the form, and I can do that for all fields except the FileFields. What is the correct way to obtain the Files uploaded in the form and use them as attributes when creating an object?

  • Why not use the `form.cleaned_data`? By using `request.POST` and `request.FILES` the cleaning phase the form does is skipped. – Willem Van Onsem Sep 08 '20 at 20:47
  • Does this answer your question? [How to upload a file in Django?](https://stackoverflow.com/questions/5871730/how-to-upload-a-file-in-django) – markwalker_ Sep 08 '20 at 20:48

0 Answers0