Process Uploaded file

Hello

First time posted and very new to Django.

I’m hoping someone can point me in the right direction here.
I’m trying to setup a django app that will accept a binary file upload, process the uploaded file, and then display the processed data.
I was able to get the upload portion to work via FormView based on many examples out there.
I also have a python class which I use for processing the file, previously used in a command line utility.
How would I pass the file to another form/view where this class is used to process the file and fill out some text boxes, menus etc, for a visual representation of the files contents?

The file does not need to be saved anywhere, it’s just used to get information out of, then discarded.
I’m not all that familiar with web frameworks, so if there is an example out there someone could point me to, it would be greatly appreciated, or even some topics to look at.

Thank you in advance
xp

As a general pattern, you would process the file in the same view as where it’s being uploaded.

However, instead of saving that file as a file, you would process the file at that point and save the necessary data into your database.

Then the view, having received a successful submission, redirects you to another url (again in the normal / typical case). In that page, you build your page from the data saved from the original submission.

If the file is small enough, you can use the MemoryFileUploadHandler, if not, you can use the TemporaryFileUploadHandler.

You’ll also want to read through File uploads and Uploaded Files and Upload Handlers. The receive_data_chunk method is going to be particularly useful here.

Thanks Ken
This puts me on the right direction and I think I can achieve what I’m trying to do.
Bit of a noob issue here. I created my model, which I want to populate from the form and send it to the DB, but I must not be understanding something as I keep getting a KeyData() got an unexpected keyword argument ‘dataSize’

Only thing of note is that KDBParser.getKeys() returns a list of dictionaries where each key is parsed out of the uploaded file, that part works fine as I can dump all the content into the console.

Here is my model:

class KeyData(models.Model):
    dataSize = models.IntegerField
    version = models.IntegerField
    keyUsageFlag = models.IntegerField
    pubExp = models.BinaryField(max_length=4)
    keyId = models.BinaryField(max_length=16, primary_key=True, unique=True)
    keySize = models.IntegerField
    # Must be all zeros
    reserved = models.BinaryField(max_length=44)
    modulus = models.BinaryField

and what I try to do with it in views.py

class FileFieldFormView(generic.FormView):
    form_class = fileUploadForm
    template_name = 'dbparser/keyDBUpload.html'
    success_url = 'keyDBHome'

    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        files = request.FILES.getlist('file_field')
        if form.is_valid():
            for f in files:
                hexdata = f.read()
                validateKeyDBImage(hexdata)
                kdb = KDBHeaderParser(hexdata)
                for key in kdb.getKeys():
                    k = KeyData(dataSize=key['DataSize'], version=key['Version'],\
                        keyUsageFlag = key['KeyUsageFlag'], pubExp=key['PubExp'], keyId=key['KeyId'],\
                        keySize=key['KeySize'], reserved=key['Reserved'], modulus=key['Modulus'])
                    k.save()
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

and finally the error that I get:

Traceback (most recent call last):
  File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\user1\Desktop\signsrv\dbparser\views.py", line 58, in post
    k = KeyData(dataSize=key['DataSize'], version=key['Version'],\
  File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 503, in __init__
    raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))

Exception Type: TypeError at /dbparser/upload
Exception Value: KeyData() got an unexpected keyword argument 'dataSize'

I tried several variations of this, but I don’t know how the model fields are supposed to get populated here, if I don’t supply any names, it seems to take the arguments, but, I don’t think that is right.

Appreciate any help.

Thank you
xp