我正在使用django 1.9。
我有一个使用以下字段的表单:
class UploadFileForm(forms.Form):
component = forms.ChoiceField(choices=[(int(x.id), x.name) for x in Component.objects.all()])
title = forms.CharField(max_length=200)
notes = forms.CharField(max_length=2000, widget=forms.Textarea(attrs={'rows': 5}))
file = forms.FileField()
当我从浏览器访问该表单时,我可以完美地使用该表单。
但是当我尝试使用curl填充表单时,我一直收到错误" 此字段是必填的"
<ul class="errorlist"><li>This field is required.</li></ul>
<p><label for="id_title">Title:</label> <input id="id_title" maxlength="200" name="title" type="text" /></p>
<ul class="errorlist"><li>This field is required.</li></ul>
<p><label for="id_notes">Notes:</label> <textarea cols="40" id="id_notes" maxlength="2000" name="notes" rows="5">
</textarea></p>
<ul class="errorlist"><li>This field is required.</li></ul>
<p><label for="id_file">File:</label> <input id="id_file" name="file" type="file" /></p>
<button type="submit"> upload file</button>
我的csrdmiddlewaretoken
被正确接受,因为我能够在响应输出中看到它。
以下是我尝试的不同curl请求:
`curl <url> \
-X POST -H "Content-Type: application/json" \
-H "Accept: text/html,application/json" \
-H "X-CSRFToken: <token grabbed from form page source>" \
-H "Cookie: csrftoken=<token grabbed from form page source>" \
-d 'title=testCurl'`
`curl <url> \
-X POST -H "Content-Type: application/json" \
-H "Accept: text/html,application/json" \
-H "X-CSRFToken: <token grabbed from form page source>" \
-H "Cookie: csrftoken=<token grabbed from form page source>" \
-F 'title=testCurl'`
`curl <url> \
-X POST -H "Content-Type: application/json" \
-H "Accept: text/html,application/json" \
-H "X-CSRFToken: <token grabbed from form page source>" \
-H "Cookie: csrftoken=<token grabbed from form page source>" \
-d '{"title":"testCurl"}'`
一旦这样做,我需要找到一种方法来传递文件字段中的文件。
任何人都可以帮我解决这个问题吗?
-----编辑: 根据@ohrstrom的建议:
当我做“卷曲”时,我会看到以下内容。来自chrome开发人员工具。
curl 'http://localhost:8000/releases/binary_upload' -H 'Cookie: JSESSIONID=84666B9EE0BB747F04AC3179FEB78F65; csrftoken=E50JjoNz1qigYUehGdxPjnsscCNaFslu' -H 'Origin: http://localhost:8000' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36' -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryn3n6mrAf19RXCh3A' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Cache-Control: max-age=0' -H 'Referer: http://localhost:8000/releases/binary_upload' -H 'Connection: keep-alive' -H 'DNT: 1' --data-binary $'------WebKitFormBoundaryn3n6mrAf19RXCh3A\r\nContent-Disposition: form-data; name="csrfmiddlewaretoken"\r\n\r\nE50JjoNz1qigYUehGdxPjnsscCNaFslu\r\n------WebKitFormBoundaryn3n6mrAf19RXCh3A\r\nContent-Disposition: form-data; name="component"\r\n\r\n13\r\n------WebKitFormBoundaryn3n6mrAf19RXCh3A\r\nContent-Disposition: form-data; name="title"\r\n\r\ntest1\r\n------WebKitFormBoundaryn3n6mrAf19RXCh3A\r\nContent-Disposition: form-data; name="notes"\r\n\r\ntest123\r\n------WebKitFormBoundaryn3n6mrAf19RXCh3A\r\nContent-Disposition: form-data; name="file"; filename="Topology_Components.png"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundaryn3n6mrAf19RXCh3A--\r\n' --compressed
但是当我从终端执行相同的命令时,它会说“提交的文件是空的&#39;
========最终编辑======== 找到了解决方案。将其添加到下面的答案中。
如果您需要更多实施细节,可以在https://github.com/kiran-vemuri/DevServe
找到答案 0 :(得分:0)
默认情况下,Django中的表单字段是必需的:
https://docs.djangoproject.com/en/1.9/ref/forms/fields/#required
您只是发送了&#39;标题&#39;数据中的字段,这是唯一没有出错的字段。
发送您要发送的数据中的所有表单字段,或者使字段必需= False。
class UploadFileForm(forms.Form):
component = forms.ChoiceField(required=False, choices=[(int(x.id), x.name) for x in Component.objects.all()])
title = forms.CharField(max_length=200)
notes = forms.CharField(required=False, max_length=2000, widget=forms.Textarea(attrs={'rows': 5}))
file = forms.FileField(required=False)
答案 1 :(得分:0)
而不是直接向网络表单发送POST
请求。我目前正在实施django-restframework
。
对于所有类型的HTTP请求viewsets
,我添加了一些额外的文件处理,现在我可以使用以下REST调用来使用curl发送数据。
`
curl -H "Content-Disposition: attachment;" \
-X POST \
-F "name=test_file" \
-F "component_id=14" \
-F "notes=Hello World how are you.." \
-F "file=@<path-to-file>" http://localhost:8000/rest/binaries/
`
答案 2 :(得分:0)
我以前只传递数据及其值,所以我只使用了这么多:
curl http://127.0.0.1:8000/api/ \
-H "Accept: application/json" \
-d '{"name":"testcurl","ph":"123456789"}'