简体   繁体   English

如何将大量数据发布到Django?

[英]How do I post a lot of data to Django?

I am trying to send of list of files to my Django Website. 我正在尝试将文件列表发送到我的Django网站。 Each set is transmitted with the following info: 使用以下信息传输每个集合:

  • File name, 文档名称,
  • File size, 文件大小,
  • File location, 档案位置,
  • File type 文件类型

Now, suppose I have 100 such sets of data, and I want to send it to my Django Website, what is the best method I Should use? 现在,假设我有100个这样的数据集,我想将它发送到我的Django网站,我应该使用的最佳方法是什么?

PS: I was thinking of using JSON, and then POST that JSON data to my Django URL. PS:我正在考虑使用JSON,然后将JSON数据发布到我的Django URL。 The data might then look like this: 数据可能如下所示:

{
  "files": [
    { "filename":"Movie1" , "filesize":"702", "filelocation":"C:/", "filetype":"avi" }, 
    { "filename":"Movie2" , "filesize":"800", "filelocation":"C:/", "filetype":"avi" }, 
    { "filename":"Movie3" , "filesize":"900", "filelocation":"C:/", "filetype":"avi" }
  ]
}

I think sending json data to your server makes sense. 我认为将json数据发送到您的服务器是有道理的。 Now to actually implement it, you would need your server to accept the http POST request that you will use to send the data about the files. 现在要实际实现它,您需要服务器接受将用于发送有关文件数据的http POST请求。

So, the server code may look like this: 因此,服务器代码可能如下所示:

urls.py: urls.py:

import myapp
#  ...
urlpatterns = patterns('', url(r'^json/$',myapp.serve_json), #http://<site_url>/json/ will accept your post requests, myapp is the app containing view functions
                         #add other urls
                      )
#other code

views.py views.py

import json
def serve_json(request):
    if request.method == 'POST':
        if 'files' in request.POST:
            file_list = json.loads(request.POST['files'])

            for file in file_list:
                #do something with each file dictionary in file_list
                #...
            return HttpResponse("Sample message") #You may return a message 
    raise Http404

Now in your Desktop application, once you have the list of the dictionaries of files, you may do this: 现在,在桌面应用程序中,一旦获得了文件词典列表,就可以执行以下操作:

import urllib,json
data = urllib.urlencode({'files':json.dumps(file_dict)}) #file_dict has the list of stats about the files
response = urllib.urlopen('http://example.com/json/', data)
print response.read()

You may also look into urllib2 and httplib and use them in place of urllib . 您也可以查看urllib2httplib并使用它们代替urllib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM