我是新用django。我已經上傳文件和FOT這個我下面目前正式文件中說明: http://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs
我index.htlm使用django上傳文件的問題
<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>
我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})
我urls.py
urlpatterns = patterns('myapp.views',
(r'^upload_file$', 'upload_file'),
(r'calculate/$', 'calculation'),
)
真的,我不知道我在做什麼錯在這裏,但似乎是條件
if request.method == "POST":
in views.py failed。即使方法=「POST」到html表單。
有人有想法嗎?
非常感謝!
嗯,我不確定。我沒有django的經驗。如果我按照你的說法嘗試去做,我會得到以下錯誤:TemplateSyntaxError:渲染時捕獲NoReverseMatch:未找到參數'()'和關鍵字參數'{}'的'upload_file'反向。 – green69 2011-03-04 10:08:48
是的,這只是因爲你的url文件應該是這樣的: 'urlpatterns = patterns('myapp.views', (r'^ upload_file $','upload_file'name ='upload_file'), (r'calculate/$','calculation',name ='calculation'), )' – fylb 2011-03-04 10:17:51
好的,謝謝。如果我修改爲fylb表示我在「name =」 – green69 2011-03-04 10:28:50