rom*_*rom 4 django upload file
是否可以使用表单中的FileField上传django中的文件,但没有模型?到目前为止,我只能找到一个模型的例子.我不想在我的数据库中为它创建一个表,我只想上传一个文件.我的表格:
class csvUploadForm(forms.Form):
csvFile = forms.FileField(label='Select a CSV file to upload.', help_text='help')
Run Code Online (Sandbox Code Playgroud)
谢谢,
罗曼
从Django文档转载的示例.
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render_to_response('upload.html', {'form': form})
def handle_uploaded_file(f):
destination = open('some/file/name.txt', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
Run Code Online (Sandbox Code Playgroud)
您可以将'some/file/name.txt'替换为您要存储该文件的其他路径.
| 归档时间: |
|
| 查看次数: |
4464 次 |
| 最近记录: |