Django FormView不在子类上重定向

时间:2017-05-25 19:06:09

标签: django class subclass

我一直试图回到OO编程的流程。当我创建UploadServerInventory类并上传文件时,我将重定向回root。但是当我创建UploadServerInventory并上传文件时,我得到一个" UploadServerInventory没有返回一个HttpResponse对象。它改为返回None。"

我不应该继承父类的所有内容吗?看起来我把一切都归还给了超级班?我错过了什么基本问题?

class UploadExcelFile(FormView):

    template_name = 'cmdb/upload.html'
    form_class = UploadFileForm
    success_url = '/'

    def form_valid(self, form):
        return super(UploadExcelFile, self).form_valid(form)

    def set_excel_file(self):
        self.excel_dict = self.request.FILES['file'].get_book_dict()

    def form_invalid(self, form):
        return super(UploadExcelFile, self).form_invalid(form)


class UploadServerInventory(UploadExcelFile):

    def form_valid(self, form):
        self.set_excel_file()
        super(UploadServerInventory, self).form_valid(form)

    def form_invalid(self, form):
        return super(UploadServerInventory, self).form_invalid(form)

1 个答案:

答案 0 :(得分:1)

您已经错过了子类form_valid方法中的return语句。

请注意,UploadExcelFile中form_validform_invalid的定义完全没有意义。没有必要重写方法只调用超类版本。