在Django python服务器上,我定制了一个用户可以上传文件的URL。现在,问题是当我点击浏览器时,我可以成功地上传文件,但是当我使用curl尝试同样的事情时,我不能这样做。
视图.pyimport json
from django.http import HttpResponse
from django.template import Context, RequestContext
from django.shortcuts import render_to_response, get_object_or_404
# -*- coding: utf-8 -*-
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from sdm.models import Document
from sdm.forms import DocumentForm
def lists(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('sdm:lists'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render_to_response(
'sdm/lists.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)
。。。。。。。。
........
........
........
列表.html
Minimal Django File Upload Example{% if documents %}
{% for document in documents %}
{{ document.docfile.name }}{% endfor %}
{% else %}
No documents.
{% endif %}
{% csrf_token %}
{{form.non_field_errors }}
{{form.docfile.label_tag }} {{form.docfile.help_text }}
{{ form.docfile.errors }}
{{ form.docfile }}
在浏览器上
在终端上,我尝试了$ curl --request PUT --upload-file filename http://wings.spectrumserver/sdm/lists
$ curl --form upload-file=filename --form press=Upload
http:// wings. spectrumserver/sdm/lists
$ curl --upload-file filename http://wings.spectrumserver/sdm/lists
$ curl --upload-file filename press=upload http://wings.spectrumserver/sdm/lists
$ curl -H 'Expect:' -F data=@filename -F submit=Upload wings.spectrumserver/sdm/lists
// In all cases, No error but no file upload
我尝试了一些其他的变化,但似乎没有结果。我还尝试了一些其他命令,这些命令给出“没有csrf令牌错误”。我也试过删除csrf token条目表单html file和setting.py,但没有任何效果。
我对curl和python都不熟悉。主要目的是使用一些python脚本上传文件。我想如果我可以通过curl上传,那么同样的东西可以用curl库在python脚本中复制,所以如果这不可行,那么有人可以建议一些python代码来上传文件到这个服务器。
编辑:$ curl -i -F name=press -F f13 wings.spectrumserver/sdm/lists
Warning: Illegally formatted input field!
curl: option -F: is badly used here
curl: try 'curl --help' or 'curl --manual' for more information
Edit2-头响应(f13是未包含的新文件)$ curl -i http://wings.spectrumserver/sdm/lists
HTTP/1.1 200可以
日期:2013年11月7日星期四23:19:18 GMT
服务器:Apache/2.2.22(Ubuntu)
变化:接受编码
内容长度:1263
内容类型:text/html;charset=utf-8
最小Django文件上载示例
- documents/2013/10
- documents/2013/11/07/list
- documents/2013/11/07/f1
- documents/2013/11/07/f12
- documents/2013/11
Select a file max. 42 megabytes