How to upload csv file using django project

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


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

链接地址: http://www.djcxy.com/p/55914.html

上一篇: Django'全球名称'错误

下一篇: 如何使用django项目上传csv文件