关于python:Django:请帮忙,我得到“is_valid()缺少1个必要的位置参数:’self’”……下面是代码块…谢谢

Django: please aid, i get “is_valid() missing 1 required positional argument:'self'”…below is the block of code…thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 class ContentCreateUpdateView(TemplateResponseMixin, View):
    module = None
    model = None
    obj = None
    template_name = 'courses/manage/content/form.html'

    def get_model(self, model_name):
       if model_name in ['text', 'video', 'image', 'file']:
           return apps.get_model(app_label='courses', model_name=model_name)
       return None
    def get_form(self, model, *args, **kwargs):
        Form = modelform_factory(model, exclude=['owner', 'order', 'created', 'updated'])
        return Form

    def dispatch(self, request, module_id, model_name, id=None):
        self.module = get_object_or_404(Module, id=module_id, course__owner=request.user)
        self.model = self.get_model(model_name)
        if id:
            self.obj = get_object_or_404(self.model, id=id, owner=request.user)

        return super(ContentCreateUpdateView, self).dispatch(request, module_id, model_name, id)

    def get(self, request, module_id, model_name, id=None):
        form = self.get_form(self.model, instance=self.obj)
        return self.render_to_response({'form': form, 'object': self.obj})

    def post(self, request, module_id, model_name, id=None):
        form = self.get_form(self.model, instance=self.obj, data=request.POST, files=request.FILES)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.owner = request.user
            obj.save()
            if not id:
                # new content
                Content.objects.create(module=self.module, item=obj)

            return redirect('module_content_list', self.module.id)
        return self.render_to_response({'form': form, 'object': self.obj})

请使用django版本:1.11.10和python版本:3.6.2。下面是回溯

回溯:

File
"C:\Users\TOCHIE\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py"
in inner
41. response = get_response(request)

File
"C:\Users\TOCHIE\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py"
in _get_response
187. response = self.process_exception_by_middleware(e, request)

File
"C:\Users\TOCHIE\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py"
in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File
"C:\Users\TOCHIE\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py"
in view
68. return self.dispatch(request, *args, **kwargs)

File
"C:\Users\TOCHIE\PycharmProjects\virtual_django\env\e_learning\educa\courses\views.py"
in dispatch
114. return super(ContentCreateUpdateView, self).dispatch(request, module_id, model_name, id)

File
"C:\Users\TOCHIE\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py"
in dispatch
88. return handler(request, *args, **kwargs)

File
"C:\Users\TOCHIE\PycharmProjects\virtual_django\env\e_learning\educa\courses\views.py"
in post
122. if form.is_valid():

Exception Type: TypeError at /course/module/4/content/image/create/
Exception Value: is_valid() missing 1 required positional argument:
'self'


你在get_form中使用的modelform_factory返回ModelForm类,在调用is_valid之前要先实例化它:

1
2
3
4
Form = self.get_form(self.model)
form = Form(instance=self.obj, data=request.POST, files=request.FILES)
if form.is_valid():
    obj = form.save(commit=False)