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

CharField


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