1
votes
 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})

please I use django version: 1.11.10 and python version: 3.6.2. Below is the traceback

Traceback:

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'

1

1 Answers

2
votes

modelform_factory which you are using in get_form returns ModelForm class, you should instantiate it before call is_valid:

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)