1
votes

I am trying to create a file upload in django.

inventory.html:

I have two buttons download_inventory button download a csv file. I am trying to upload a file using upload_inventory button.

<form method="POST" action="{% url 'bulkInventory' %}">
    {% csrf_token %}
        <div class="panel-body">
            <div class="pad-btm form-inline">
                <div class="row">
                    <div class="col-sm-6 table-toolbar-left">
                        <button name="download_inventory" id="download_inventory" class="btn btn-purple btn-labeled fa">Download Inventory</button>
                        &nbsp;&nbsp;
                        <input type="file" class="btn btn-purple" name="inventory_csv" >
                        &nbsp;&nbsp;
                        <button name="upload_inventory" id="upload_inventory" class="btn btn-purple btn-labeled fa dropzone" >Upload Inventory</button>
                    </div>
                    .....

urls.py:

method bulkInventory is mapped with action bulkInventory.

urlpatterns = patterns('',
                    url(r'^$', views.inventory, name='inventory'),
                    url(r'^product.html/', views.product, name='product'),
                    url(r'^update/', views.updateSingle, name='update'),
                    url(r'^inventory/', views.bulkInventory, name='bulkInventory'),)

views.py:

def bulkInventory(request):
    api = APIMethods()
    if request.method == 'POST' and 'download_inventory' in request.POST:
        api.downloadInventory()
        inv_file = open('inventory_sheet.csv', 'rb')
        response = HttpResponse(inv_file, content_type='application/csv')
        response['Content-Disposition'] = "attachment; filename=inventory_sheet.csv"
        os.system('rm inventory_sheet.csv')
        return response

    if request.method == 'POST' and 'upload_inventory' in request.POST:
        form = forms.UploadFile(request.POST, request.FILES)
        print form.is_valid()
        api.saveUploadedInventory(request.FILES['inventory_csv'])    #  this method saves the content of file
        return HttpResponseRedirect(reverse('inventory'))

forms.py:

class UploadFile(forms.Form):
    inventory_file = forms.FileField()

It is giving me this error:

Traceback: File "/home/manish/syserp/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 132.

response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/manish/syserp/ekomerz/inventory/views.py" in bulkInventory 78. api.saveUploadedInventory(request.FILES['inventory_csv']) File "/home/manish/syserp/local/lib/python2.7/site-packages/django/utils/datastructures.py" in getitem 322. raise MultiValueDictKeyError(repr(key))

Exception Type: MultiValueDictKeyError at /product/inventory/ Exception Value: "'inventory_csv'"

1
missing <form .. enctype="multipart/form-data" ..>?chfw
@chfw still getting same errorManish Gupta
use 'inventory_file' as key instead of 'inventory_csv'.chfw
If i will use inventory_file as key, How the file input element will be reference in the method? I used it and still it is throwing same error.Manish Gupta
UploadFile got inventory_file as key but in your html form it was named inventory_csv. to avoid this, you can do form = UploadFile() then in your html template, place this line {{ form.as_table }}.chfw

1 Answers

1
votes

Why dont you go with form.cleaned_data[''inventory_csv] after form.is_valid()

Before that to check what you are getting print form.cleaned_data['inventory_csv']

Hope that wil help