Python‎ > ‎django‎ > ‎django.forms‎ > ‎

EmailField


Here, in this examples, the form validation is processed through the calling the function in the forms.EmailField(). The  function validated the list items after the form is submitted through POST method.
 Steps for creating virtual environment, startproject, startapps, & running server:
  • virtualenv myenv
  • cd  myenv
  • .\Scripts\activate
  • .\Scripts\deactivate
  • pip install django
  • python .\Scripts\django-admin.py startproject mysite
  • cd mysite
  • python manage.py startapp myapp
  • python manage.py runserver

Files


setting.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles', 'myapp', 
)

urls.py

from myapp.views import submit

urlpatterns = [
    url(r'^', submit),
]

myapp/forms.py

from django import forms
from django.core.exceptions import ValidationError

def Validate(email):
    list=['adil@adilwaseem.com', 'adil@pythonlake.com','contact@adilwaseem.com']
    if email not in list:
        raise ValidationError("%s is not in our record."% email )

class AccessForm(forms.Form):
    email=forms.EmailField(label='Email',validators=[Validate])

myapp/views.py

from django.shortcuts import render

from .forms import AccessForm

def submit(request):
    if request.method == 'POST':
        form=AccessForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/thanks/')
    else:
        form = AccessForm()

    return render(request, 'myapp\index.html', {'form':form})

myapp/index.html

<!DOCTYPE html>
<html>
<title> </title>
<body>

<form action="/Response/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

</body>
</html>

view at localhost:8000/








Here, the submit form is created through the django web framework using django.forms. The forms is handled through the HTML files in which the views.py variables data is processed. While views.py import the form fields from myapps/forms.py
Steps for creating steps for virtua 

Steps for creating virtual environment, startproject, startapps, & running server:
  • virtualenv myenv
  • cd  myenv
  • .\Scripts\activate
  • .\Scripts\deactivate
  • pip install django
  • python .\Scripts\django-admin.py startproject mysite
  • cd mysite
  • python manage.py startapp myapp
  • python manage.py runserver

Files



Settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles', 'myapp'
)


mysite/urls.py

from django.conf.urls import url
from myapp.views import submit

urlpatterns = [
    url(r'^', submit),
]


myapp/forms.py

__author__ = 'adilwaseem75'

from django import forms

class CharField(forms.Form):
    username=forms.CharField()
    username.label="Username"
    username.max_length=50

class EmailField(forms.Form):
    email=forms.EmailField()
    email.label="Email"
    email.max_length=50


myapp.views.py

from django.shortcuts import render

from .forms import EmailField
from .forms import CharField

def submit(request):
    username=CharField()
    email=EmailField()

    return render(request, 'myapp\index.html', {'username':username, 'email':email})

myapp.index.html

<!DOCTYPE html>
<html>
<title> </title>
<body>

<form action="/Thank-you/" method="post">
    {% csrf_token %}
    {{username}} <br> <br>
    {{email}}<br><br>
    <input type="submit" value="Submit" />
</form>

</body>
</html>


Running the server through Window CMD command, python manage.py runserver