Forms CGI

Posted under » Django on 18 Jun 2021

After creating a form layout, let us make the form's back end (in Perl we call it the CGI). In Django, the "CGI" is in the

As mentioned, this CGI or application is like a page. If the CGI is "self" then you edit the form def itself. Otherwise you have to create another def by adding the URLconf to the which is connected to the which may look like this.

  path('vote',, name='vote'),

We already learnt creating a minimal form and now we also have learnt how to use the forms class to do the form layout. Combining what we have learnt, the CGI may look like this.

from django.http import HttpResponse
from django.shortcuts import render
from .models import Questionnaire
from .forms import VoteForm

def index(request):
    return HttpResponse("Hello, world. You're at the feel index.")

def q1(request):
    form = VoteForm()
    return render(request, 'feel/q1.html', {'form': form})

def vote(request):

    if request.method=='POST':
        form = VoteForm(request.POST)
        if form.is_valid():
            q_id = form.cleaned_data['q_id']
            rating = form.cleaned_data['rating']
        form = VoteForm()

        return render(request, 'feel/q1.html')

Note the import models and forms.

Def q1 is the form and vote is the CGI.

Only if post is submitted and valid then we do vote. is the command to save to database. Django takes care of all the MySQL insert, update and stuff.

After which, it will go back to template q1 but no form is defined.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data