One of the easiest ways to create a form is like this:
models.py
from django.db import models class Contact(models.Model): name = models.CharField(max_length=100) email = models.EmailField() message = models.CharField(max_length=999) def __str__(self): return self.name
forms.py
from django.forms import ModelForm class ContactForm(ModelForm): required_css_class = "required" class Meta: model = Contact fields = "__all__"
views.py
from .models import Contact def contactView(request): submitted = False if request.method == "POST": form = ContactForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect("?submitted=True") else: form = ContactForm() if "submitted" in request.GET: submitted = True return render( request, "contact.html", {"form": form, "page_list": Contact.objects.filter(), "submitted": submitted}, )
You can learn more about handling forms on the documentation.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
One of the easiest ways to create a form is like this:
models.py
forms.py
views.py
You can learn more about handling forms on the documentation.