DEV Community


Posted on


Form submission

Hello everyone, I need some help. Im creating a website and I'd like it when a visitor visits the contact page and fills out a contact form it will go to a email address which will be configured with a hosting company. Any help on this subject will be much appreciated and many thanks in advance

Top comments (1)

highcenburg profile image
Vicente Antonio G. Reyes

One of the easiest ways to create a form is like this:

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):

from django.forms import ModelForm

class ContactForm(ModelForm):
    required_css_class = "required"

    class Meta:
        model = Contact
        fields = "__all__"

from .models import Contact

def contactView(request):
    submitted = False
    if request.method == "POST":
        form = ContactForm(request.POST)
        if form.is_valid():
        return HttpResponseRedirect("?submitted=True")
        form = ContactForm()
        if "submitted" in request.GET:
            submitted = True

    return render(
        {"form": form, "page_list": Contact.objects.filter(), "submitted": submitted},

You can learn more about handling forms on the documentation.