[Answered ]-Convert function based view to class based view django

1👍

You can use a FormView [Django-doc]:

from django.views.generic.edit import FormView
from .forms import contact_form

class ContactUsView(FormView):
    form_class = contact_form
    template_name = 'name-of-template.html'

    def form_valid(self, form):
        return HttpResponse('Data is valid')

    def form_invalid(self, form):
        return HttpResponse('Data is invalid')

For a GET request, the default behavior will be to render the template_name with as form variable the form object of the specified form_class.


Note: Forms in Django are written in PascalCase, not snake_case,
so you might want to rename the model from contact_form to ContactForm.

Leave a comment