我是django的新手。我要上传一个文件,并按照官方文档中的说明进行操作:
http://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs
我的index.htlm
<form action="upload_file" enctype="multipart/form-data" method="POST">
{% csrf_token %}
<input type="file" name="upfile" size="30">
<input type="submit" name="upfile" value= " Upload ">
</form>
my views.py:
def handle_uploaded_file(f):
destination = open('/my_path_to_tmp/tmp_files/input_file', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
if ( f.file_name.endswith("sdf") ):
return "sdf"
elif ( f.file_name.endswith("smi") ):
return "smi"
def upload_file(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
file_type = handle_uploaded_file(request.FILES['upfile'])
return HttpResponseRedirect('calculate', file_type)
else:
form = UploadFileForm()
return render_to_response('upload.html', {'form': form})
my urls.py
urlpatterns = patterns('myapp.views',
(r'^upload_file$', 'upload_file'),
(r'calculate/$', 'calculation'),
)
我真的不知道我在这里做错了什么,但似乎条件
if request.method == "POST":
views.py中的失败。即使方法=“POST”到html表格。
有人有想法吗?
非常感谢你!
答案 0 :(得分:1)
您确定自己的表单操作是否正确?
不应该是这样的事情:
<form action="{% url upload_file %}" enctype="multipart/form-data" method="post">
答案 1 :(得分:0)
您可以在方法的开头输出request.method,只是为了确保...之后,打印form._errors。