无法将CSV文件上传到Django中的Elastic Search

时间:2019-10-30 14:20:31

标签: python django csv elasticsearch-5

我正在尝试使用以下代码将CSV文件上传到Django中的Elasticsearch:

uploadfile.html

export default {
  // the name becomes the reducer name in the resulting state
  name: 'users',
  // the Redux reducer function
  reducer: (state = [], action) => {
    // ...
    return state
  },
  // anything that starts with `select` is treated as a selector
  selectActiveUsers: state => state.users.filter(user => user.isActive),
  // anything that starts with `do` is treated as an action creator
  doUpdateUser: (userId, attrs) => ({ dispatch, apiFetch }) =>
    dispatch({ type: 'USER_UPDATE_STARTED' })
    apiFetch('/users', { type: 'PUT' }, attrs)
      .then(res => {
        dispatch({ type: 'USER_UPDATE_FINISHED' })
      })
      .catch(err => {
        dispatch({ type: 'USER_UPDATE_FAILED' })
      }),
  // optional init method is ran after store is created and passed the
  // store object.
  init: store => {
    // action creators are bound and attached to store as methods
    store.doUpdateUser()

    // selectors are also "bound" and attached to store as methods
    store.selectActiveUsers()
  }
}

其中,indexfile是urls.py文件中的路径

<body>
<form action="indexfile" method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="file" name="myfile">
    <input type="submit">
</form>
</body>

views.py

from django.urls import path
from . import views
urlpatterns = [
    path('indexfile', views.indexfile)
]

但是,它给了我以下错误

def indexfile(request):
    from elasticsearch import Elasticsearch, helpers
    import csv
    es = Elasticsearch(<endpoints>)

    es.indices.create(index='demo', ignore=400)

    reader = csv.DictReader(request.FILES['myfile'])
    helpers.bulk(es, reader, index="demo", doc_type='mytype')

    return render(request, 'add/result.html')

如何解决此错误?

0 个答案:

没有答案