[Fixed]-Get Table selection and File in same form POST

1👍

I updated your loadlocndb view:

@login_required   
def loadlocndb(request):


if request.method == "POST":
    pks = request.POST.getlist("update")

    myfile = request.FILES['form_field_name'].file
    print pks, myfile

    selected_objects = Vehicle.objects.filter(pk__in=pks)

    vlist = []
    for i in selected_objects:
        vlist.append(i)

    return render(request, 'portal/loadlocndb.html',{"vlist":vlist})

I haven’t tested it, but this: request.FILES['form_field_name'].file
will get the file from your form. Just replace your file form field name with 'form_field_name'.

Can you please put your code from your form.py?

And in your template, you should replace <form action="/loadlocndb/" method="POST"> with <form action="/loadlocndb/" method="POST" enctype="multipart/form-data">.

From documentation: Note that request.FILES will only contain data if the request method was POST and the <form> that posted the request has the attribute enctype=”multipart/form-data”. Otherwise, request.FILES will be empty.

👤arrt_

Leave a comment