Django Drama: (Drag-n-Drop File Uploader in Multi-

2019-09-16 14:26发布

问题:

There are a few tiny related questions buried in here, but they really point to one big, hairy best practice question. This is kind of a tough feature to implement because it's supposed to do a couple tricky things at once...

  • drag-and-drop multi-file uploader (via Javascript)
  • multi-page form (page one: upload and associate files with an existing document model; page two: update and save file/document objects and meta-data to database)

...and I haven't found a pre-existing code sample or implementation anywhere. (Depending on one's approach, it could sweep off the table or automagically answer all the related/embedded/follow-on questions.) Bottom-line, the purpose of this post is to answer this question: What's the most elegant approach which minimizes the intervening questions/problems?

I'm using this implementation of a drag-and-drop JQuery File Uploader in Django to upload files...

https://github.com/miki725/Django-jQuery-File-Uploader-Integration-demo

The solution I link to above saves files on the filesystem, of course, but in batches per upload session, via creating a directory for each batch of files, and then assigning a UUID to each of those directories. Each uniquely named directory on the filesystem contains files uploaded during that particular upload session. That means any sort of database storage method first has to tease apart and iterate over all the files in the filesystem directory created for each upload session by this solution.

Note: the JQuery solution linked to above doesn't use a form (in forms.py) inside the app directory. The form is hardcoded into the template, which is already a bit of a bummer...'cause now I also have to find a nice way to bind each of the above files in each batch to a form.

I think the simplest--albeit perhaps least performant solution--is to create two views, for two forms...to save each file to the database in the view on the first page, and then update the database on the second page. Here's the direction I'm presently rolling in:

IN THE TEMPLATE...

...uploader javascripts in header...

<form action="{% url my_upload_handler %}" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple
</form>

IN VIEWS.PY...

def my_upload_handler_0r_form_part_one(request):

# POST (in the upload handler; request triggered by an upload action)

    if request.method == 'POST':
        if not ("f" in request.GET.keys()):

          ...validators and exception handling...
          ...response_data, which is a dict...

            uid = request.POST[u"uid"]

            file = request.FILES[u'files[]']
            filename = os.path.join(temp_path, str(uuid.uuid4()) + file.name)
            destination = open(filename, "wb+")
            for chunk in file.chunks():
                destination.write(chunk)
            destination.close()

            response_data = simplejson.dumps([response_data])
            response_type = "application/json"
            # return the data to the JQuery uploader plugin...
            return HttpResponse(response_data, mimetype=response_type)

# GET (in the same upload handler)

    else:    
            return render_to_response('my_first_page_template.html',
                              {                     <---NO 'form':form HERE
                              'uid': uuid.uuid4(),
                              },
                              context_instance = RequestContext(request))


def form_part_two(request):

    #here I need to retrieve and update stuff uploaded on first page

    return render_to_response('my_second_page_template.html', 
    {}, 
    context_instance = RequestContext(request))

This view for the first page leverages the JQuery uploader, which works great for multi-file uploads per session and does what it's supposed to do. However, as hinted above, the view, as an upload handler, is only the first page in what needs to be a two page form. On page two, the end user would subsequently need to retrieve each uploaded file, attach additional data to the files they just uploaded on page one, and re-save to the database.

I've tried to make this work as a two-part form via various solutions, including form wizards and/or generic class based views...following examples mainly enabling data persistence via the session. These solutions get rather thorny very quickly.

In summary, I need to...

  • upload multiple files in a uniquely identified batch (via drag and drop)
  • tease apart and iterate over each batch of uploaded files
  • bind each file in the batch to a form and associate it with an existing document model
  • submit / save all of these files at once to the database
  • retrieve each of those files on the following page/template of a potentially new form
  • update metadata for each file
  • resubmit / save all of those files at once to the database

So...you can see how all of the above compounds the complexity of a simple file upload, and increases the complexity of providing the feature, by involving related questions like:

forms.py: how best to bind each file to a form

models.py: how to associate each file with a pre-existing document model

views.py how to save each file in accordance with pre-existing document model in Postgres in the first page; update and save each document in the second page

...and, again, I'd like to do all of that without a form wizard, and without class-based views. (CBVs, especially, for this use case elude me a bit.) In other words: I'm looking for advice leading toward the most bulletproof and easy to read/understand solution possible. If it causes multiple hits to the database, that's fine by me. (If saving a file to the database seems anti best practice, please see this other post: Storing file content in DB

Might I be able to just create a separate view for two forms, and subclass a standard upload form, like so...

In forms.py...

class FileUploadForm(forms.Form):

    files = forms.FileField(widget=forms.ClearableFileInput(attrs={'name':'files[]', 'multiple':'multiple'}))
    #how to iterate over files in list or batch of files here...?
        file = forms.FileField()

    file = forms.FileField()

    def clean_file(self):
        data = self.cleaned_data["file"]

        # read, parse, and create `data_dict` from file...
        # subclass pre-existing UploadModelForm      
        **form = UploadModelForm(data_dict)**

        if form.is_valid():
            self.instance = form.save(commit=False) 
        else:
            raise forms.ValidationError
        return data

...and then refactor the earlier upload handler above with something like...

In views.py, substituting the following for present upload handler...

    def view_for_form_one(request):  
        ...
        # the aforementioned upload handler logic, plus...
        ...
        form = FileUploadForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
        else:
            # display errors
            pass
        ...

    def view_for_form_two(request):

        # update and commit all data here

...?

回答1:

In general, with this type of problem, I like to create single page with one <form> on it, but multiple sections which the user progresses through with javascript.

Breaking a form into a multi-part, wizard-style form series is much easier with javascript, especially if the data it produces is dynamic in nature.

If you absolutely must break it out into multiple pages, I would advise you to set up your app to be able to save the data into the database at the end of each step.

You can do that by making the metadata which the user adds at step 2 a nullable field, or even moving the metadata to a separate model.