我正在尝试将文本文件上传到指定位置,但该文件在根目录中上传空(不在我指定的位置)。
在模特中:
course = models.FileField(help_text=('Upload a course (max %s kilobytes)' %settings.MAX_COURSE_UPLOAD_SIZE),upload_to='cfolder/',blank=True)
表格:
def handle_uploaded_file(f):
destination = open('Cfolder')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
在观点中:
def save_course(request, classname):
classroom = Classroom.objects.get(classname = classname)
if request.method == 'POST':
form = CoursesForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['course'])
new_obj = form.save(commit=False)
new_obj.creator = request.user
new_obj.classroom = classroom
new_obj.save()
return HttpResponseRedirect('.')
else:
form = CoursesForm()
return render_to_response('courses/new_course.html', {
'form': form,
},
context_instance=RequestContext(request))
我猜我的错误在handle_uploaded_file方法中。 我应该如何修改它才能正常工作? 谢谢!
答案 0 :(得分:6)
您是否记得将enctype="multipart/form-data"
放入HTML <form>
代码?
答案 1 :(得分:2)
调用handle_uploaded_file
函数??
默认情况下form.save
会将文件保存在upload_to
目录