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

2024-04-25 21:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试发送文件列表到我的Django网站。每一组传输信息如下:

  • 文件名
  • 文件大小
  • 文件位置
  • 类型文件

现在,假设我有100组这样的数据,我想把它发送到我的Django网站,我应该使用什么样的最佳方法?

PS:我在考虑使用JSON,然后将JSON数据发布到我的Django URL。数据可能如下所示:

{
  "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" }
  ]
}

Tags: 文件数据django方法信息json类型列表
1条回答
网友
1楼 · 发布于 2024-04-25 21:26:57

我认为向服务器发送json数据是有意义的。现在要真正实现它,您需要您的服务器接受httpPOST请求,您将使用该请求发送有关文件的数据。在

因此,服务器代码可能如下所示:

在网址.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

在视图.py在

^{pr2}$

现在,在桌面应用程序中,一旦有了文件字典的列表,就可以执行以下操作:

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()

您还可以研究urllib2和{},并用它们代替{}。在

相关问题 更多 >