1

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'"

5
  • missing <form .. enctype="multipart/form-data" ..>? Commented Sep 23, 2015 at 12:11
  • @chfw still getting same error Commented Sep 23, 2015 at 12:23
  • use 'inventory_file' as key instead of 'inventory_csv'. Commented Sep 23, 2015 at 12:34
  • 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. Commented Sep 23, 2015 at 12:44
  • 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 }}. Commented Sep 23, 2015 at 13:04

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

inv_file = form.cleaned_data['inventory_csv']. On doing this giving me this error: Exception Type: AttributeError at /product/inventory/ Exception Value: 'UploadFile' object has no attribute 'cleaned_data'
that means your form is not valid
if request.method == "POST": form = UploadFile(request.POST, request.FILES) if form.is_valid(): inv_file = form.cleaned_data['inventory_csv']
i did that. and it says false when i print form.is_valid(). I have included forms.py. How to correct the error?
Your form should work if you have uploaded the csv file from your form. and your uploaded file is not corrupt. If it doesnt work try changing the class name of the form and view.. I had same issue once and it solved me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.