[Answered ]-How form action works in django when using a class in views.py for form creation

2đź‘Ť

âś…

Dont be afraid to read django’s source code :P, the generic class has two methods: “get” and “post” (and “put” too, but it calls “post”) you can overwrite any of them if you need to.

class BaseCreateView(ModelFormMixin, ProcessFormView):
    """
    Base view for creating an new object instance.

    Using this base class requires subclassing to provide a response mixin.
    """
    def get(self, request, *args, **kwargs):
        self.object = None
        return super(BaseCreateView, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.object = None
        return super(BaseCreateView, self).post(request, *args, **kwargs)

but it also inherits the methods of it’s parents so it could be a little hard to read. I always check the docs for the generic views, it gives you a list of all the methods that you can overwrite on every generic class. Now you can overwrite all the methods you want without repeating code (that’s why I <3 CBV)

I think in your case you might want to overwrite the form_valid() method to do something before redirecting to the success page

Hope this helps

Leave a comment