Rendering/Raising A Validation Error on an HTML Template in Django
<p>I'm attempting to list a validation error on my HTML template if the form on that template isn't properly submitted. My forms.py file includes a function (clean_email) that will catch any string that's entered that doesn't include an "@" symbol, and it actually works. Therefore, any form that I attempt to submit without an "@" character in the email field won't be submitted. </p>

<p>Unfortunately, the error isn't displayed as it should be on my HTML template. The code for displaying it is in line 15 of contact.html (contactform.errors ). This code doesn't produce any results. Here are the relevant files:</p>

<p>Models.py</p>

<pre><code>from __future__ import unicode_literals
from django.db import models

class Contact(models.Model):

    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    phone_number = models.CharField(max_length=30, blank=True)
    email = models.CharField(max_length=50)
    message = models.TextField(default=" ")
</code></pre>

<p>Forms.py</p>

<pre><code>from models import Contact
from django.forms import ModelForm
from django.core.exceptions import ValidationError
from django import forms

class ContactForm(ModelForm):

    class Meta:
        model = Contact
        fields = "__all__"

    def clean_email(self):
        email = self.cleaned_data["email"]

        if "@" not in email:
            raise ValidationError("Not an Email Address")

        return email
</code></pre>

<p>Views.py</p>

<pre><code>from django.shortcuts import render
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from forms import ContactForm

def contact(request):
    contactform = ContactForm()
    if request.method == "POST":
        contactform = ContactForm(request.POST)
        if contactform.is_valid():
            message = contactform.save(commit=False)
            message.save()
            return HttpResponseRedirect(reverse("contact"))
        else:
            print(contactform.errors)
            contactform = ContactForm()
    return render(request,"contact_form/contact.html",{"contactform":contactform})
</code></pre>

<p>Finally, Contact.HTML</p>

<pre><code>{% block content %}

 &lt;link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"&gt;
 &lt;link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"&gt;

  &lt;link rel="stylesheet" href="{% static "css/main.css" %}"&gt;
  &lt;link rel="stylesheet" href="{% static "css/contact.css" %}"&gt;

  &lt;div class="outer"&gt;
      &lt;form method="POST" action=""&gt;
        {% csrf_token %}
          &lt;ul&gt;
            &lt;li&gt;
                &lt;label = for="email"&gt;Email&lt;/label&gt;
                {{ contactform.email }}
                {{ contactform.errors }}
            &lt;/li&gt;
          &lt;/ul&gt;
        &lt;input type="submit" value="Submit Form"/&gt;
      &lt;/form&gt;
  &lt;/div&gt;

{% endblock %}
</code></pre>

<p>Any ideas/help is appreciated, thanks!</p>