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 fromtocontact_form
ContactForm
.
Source:stackexchange.com