【问题标题】:Uploading files from a Python client to a Django Server将文件从 Python 客户端上传到 Django 服务器
【发布时间】:2014-04-05 18:34:46
【问题描述】:

我正在尝试将一些文件从 Python 客户端上传到 Django Web 应用程序。

我可以通过使用表单来做到这一点,但我不知道如何使用独立的 Python 应用程序来做到这一点。你能给我一些建议吗?

我正在像这样在 Django 模型中对文件进行建模:

class Media(models.Model):
    post = models.ForeignKey(Post)
    name = models.CharField(max_length=50, blank=False,null=False)
    mediafile = models.FileField(upload_to=media_file_name, blank=False,null=False)

干杯。

【问题讨论】:

    标签: python django file post upload


    【解决方案1】:

    您要做的是向 Django 应用发送一个POST 请求,在其中发送一个文件。

    您可以使用 python 的标准库 httplib module 或第 3 方 requests module。发布的最后一个链接显示了如何发布多部分编码文件,这可能是您需要的。

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      使用requests

      with open('file') as f:
          requests.post('http://some.url/upload', data=f)
      

      【讨论】:

        【解决方案3】:

        它现在使用 Python 的 requests 模块实际上正在工作

        我会把代码给所有感兴趣的人...

        Django 服务器...

        urls.py

        ...
        url(r'^list/$', 'dataports.views.list', name='list'),
        ...
        

        views.py

        @csrf_exempt
        def list(request):
            # Handle file upload
            if request.method == 'POST':
                print "upload file----------------------------------------------"
                form = DocumentForm(request.POST, request.FILES)
                if form.is_valid():
                    print "otra vez.. es valido"
                    print request.FILES
        
                    newdoc = Jobpart(
                                        partfile = request.FILES['docfile']
                    )
                    newdoc.save()
        
                    # Redirect to the document list after POST
                    return HttpResponseRedirect(reverse('dataports.views.list'))
            else:
                #print "nooooupload file----------------------------------------------"
                form = DocumentForm() # A empty, unbound form
        
        
            # Render list page with the documents and the form
            return render_to_response(
                'data_templates/list.html',
                {'form': form},
                context_instance=RequestContext(request)
            )
        

        list.html

        <!DOCTYPE html>
        <html>
            <head>
                <meta charset="utf-8">
                <title>Minimal Django File Upload Example</title>   
            </head>
        
            <body>
                <!-- Upload form. Note enctype attribute! -->
                <form action="{% url "list" %}" method="post" enctype="multipart/form-data">
                    <p>
                        {{ form.docfile }}
                    </p>
                    <p><input type="submit" value="Upload" /></p>
                </form>
        
            </body>
        
        </html> 
        

        现在在客户端中。

        client.py

        import requests
        url = "http://localhost:8000/list/"
        response = requests.post(url,files={'docfile': open('test.txt','rb')})
        

        现在你可以添加一些安全性和东西了。但它实际上是一个非常简单的例子。

        谢谢大家!!!!

        【讨论】:

          【解决方案4】:
          file = request.FILES['file']
          
          load_file = FileSystemStorage()
          
          filename = load_file.save(file.name, file)  // saving in local directory and getting filename
          
          data = {'name': name, 'address': address, 'age':age }
          
          fr_data = None
          with open(filepath ,'rb') as fr:
              fr_data += fr.read()
          
          url = 'http://127.0.0.1:8000/api/'
          
          response = requests.post(url=url, data=data, files= {
                                    'filefiledname': fr_data
                                    }
                                   )
          
          

          【讨论】:

          • 当您回答超过 5 年的帖子时,如果您可以在答案中添加一些上下文以及与其他答案相比添加的内容,而不是仅仅发布代码,那会更好。
          猜你喜欢
          • 1970-01-01
          • 2011-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-11
          • 1970-01-01
          相关资源
          最近更新 更多