Django自定义上传目录

由于数据库的upload_to功能,有时不能满足每次上传灵活自定义的需求,

基于DEF的上传,有时不能满足基于CLASS的视图要求,

于是,只好慢慢用土法实现。

当然,首先,要使用上传功能时,form里的这个东东enctype="multipart/form-data",必须加。

然后,关键实现如下:

class UploadView(FormView):
    template_name = 'upload.html'
    form_class = UploadFileForm

    def form_invalid(self, form):
        return self.render_to_response(RequestContext(self.request, {'form': form}))

    def form_valid(self, form):
        file_path = form.cleaned_data['file_path']#这里保存着我们要的东东哟
        deployversion_upload_done(file_path)
        return HttpResponseRedirect(reverse("list"))

def deployversion_upload_done(upload_file):

        file_with_path = "这里可以传变量或自定义目录哟" + str(upload_file)
        with open(file_with_path, 'wb+') as destination:
            for chunk in upload_file.chunks():
                destination.write(chunk)
上一篇:flex的http URL转码与解码


下一篇:mac mono Fiddler.exe启动失败