Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3990828
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:32:30+00:00 2026-05-20T06:32:30+00:00

What is the best way to allow a user to do an arbitrary number

  • 0

What is the best way to allow a user to do an arbitrary number of file uploads with Django?

If I knew I was going to allow the user to do up to two file uploads then I could just do this in my forms.py…

from django import forms

class UploaderForm(forms.Form):
    file1 = forms.FileField(upload_to='user_files')
    file2 = forms.FileField(upload_to='user_files')

…and this in my views.py…

...
if request.method == 'POST':
    form = UploaderForm(request.POST)
    if form.is_valid():
        form.save()
        # do something.
else:
    form = UploaderForm()
...

However, I’m planning on using Javascript on the frontend to allow the user to continually add new <input type="file" name="fileN" /> elements (where N is the Nth file upload field added to the page.) The user can choose to do their upload with one or more files selected.

Is there any way that I can use Django’s built in forms to accomplish this?

If, as I suspect, I can’t utilize Django forms to generate the forms, can I use Django’s validation? i.e., it would be handy to be able to access form.cleaned_data['file0'], form.cleaned_data['file1'], etc., instead of having to do my own validation.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:32:31+00:00Added an answer on May 20, 2026 at 6:32 am

    Formsets are exactly what you need.
    http://docs.djangoproject.com/en/dev/topics/forms/formsets/#formsets

    All you have to do is follow the field naming format that the formset uses 'form-0-field' in your javascript instead of fileN

    Also modify the form-TOTAL_FORMS accordingly. Check the docs, they are especially thorough on this one.

    class UploaderForm(forms.Form):
        file1 = forms.FileField(upload_to='user_files')
    
    
    from django.forms.formsets import formset_factory
    UploaderFormset = formset_factory(UploaderForm)
    
    # long as you specify 'form-TOTAL_FORMS' and 2 other fields listed in the docs,
    # the formset will auto generate form instances & populate with fields based on
    # their 0 index.
    formset = UploaderFormset(request.POST)
    
    for form in formset.forms:
        form.save()
    

    The best part is that this ties into validation perfectly, and you could even re-display your forms for error messages (which would be a bit of a pain if you were only generating html via ajax)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way to allow a user to browse for a file in
What is best way to allow user to pick date from a mobile device
Best way to allow the User to define a Table’s Order? We are using
What is the best way to allow the user to access their music library
What is the best way to allow a team of programmers to use Netbeans,
Hey, anyone have any idea what the best way to allow users to save
Right now I have two activities. My goal is to allow the user to
What is the best way to disable the warnings generated via _CRT_SECURE_NO_DEPRECATE that allows
What is the best way to verify/test that a text string is serialized to
What is the best way of creating an alphabetically sorted list in Python?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.