views.py
import 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代码片段,其中包含一个段落标签和一个加粗的标题标签(strong),“lists.html”。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Minimal Django File Upload Example</title>
</head>
<body>
<!-- List of uploaded documents -->
{% if documents %}
<ul>
{% for document in documents %}
<li><a href="{{document.docfile.url }}">{{ document.docfile.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No documents.</p>
{% endif %}
<!-- Upload form. Note enctype attribute! -->
<form action="{% url sdm:lists %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{form.non_field_errors }}</p>
<p>{{form.docfile.label_tag }} {{form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
</p>
<p><input type="submit" name="press" value="Upload" /></p>
</form>
</body>
</html>
在浏览器上:
在终端上,我尝试了
$ 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 令牌错误”。我也试着从html文件和setting.py中删除csrf token条目,但都没有起作用。
我对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
编辑2-头部响应(f13是新文件,未包含)
$ curl -i http://wings.spectrumserver/sdm/lists
HTTP/1.1 200 OK 日期:2013年11月7日 星期四 23:19:18 GMT 服务器:Apache/2.2.22 (Ubuntu) 变化:接受编码 内容长度:1263 内容类型:text/html; charset=utf-8
最小的Django文件上传示例
<ul>
<li><a href="/media/documents/2013/10/28/templates.zip">documents/2013/10
/28/templates.zip</a></li>
<li><a href="/media/documents/2013/11/07/list">documents/2013/11/07/list</a>
</li>
<li><a href="/media/documents/2013/11/07/f1">documents/2013/11/07/f1</a></li>
<li><a href="/media/documents/2013/11/07/f12">documents/2013/11/07/f12</a></li>
<li><a href="/media/documents/2013/11/07/hello.html">documents/2013/11
/07/hello.html</a></li>
</ul>
<!-- Upload form. Note enctype attribute! -->
<form action="/sdm/lists" method="post" enctype="multipart/form-data">
<!--
--> <p></p>
<p><label for="id_docfile">Select a file</label> max. 42 megabytes</p>
<p>
<input type="file" name="docfile" id="id_docfile" />
</p>
<p><input type="submit" name="press" value="Upload" /></p>
</form>
</body>
</html>