How to Create Django Form Manually

0
31

Working Example:

urls.py


from django.contrib import admin
from django.conf.urls import url
from form_app import views

urlpatterns = [
 url(r'^$', views.index, name='index'),
 url(r'^admin/', admin.site.urls),
 url(r'^form-url-page/', views.form_name_view, name='form_page' ),
]


 

 

forms.py


from django import forms

class basicform(forms.Form):
 name = forms.CharField()
 email = forms.EmailField()
 text = forms.CharField(widget = forms.Textarea)


 

 

views.py


from django.shortcuts import render
from form_app import forms

# Create your views here.

def index(request):
 return render(request, 'form_app/index.html')

def form_name_view(request):
 form = forms.basicform()
 if request.method == 'POST':
 form = forms.basicform()

if form.is_valid():
 print('validation Success!')
 print("Name: "+form.cleaned_data['name'])
 print("Email: "+form.cleaned_data['email'])
 print("Text: "+form.cleaned_data['text'])
 return render(request, 'form_app/form_page.html', {'form' : form})

Rendering Process


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Forms</title>
</head>
<body>
<div class="container">
<form method="post">
{{form.as_p}}
{% csrf_token %}
<input type="submit" class="btn btn-primary" value="Submit" />
</form>
</div>
</body>
</html>

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here