Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 How to create a single method to load templates and write code?

Django provides a single view method to render the template using the GET method and POST method to write code after form submission.

It is the standard approach to write Django Code.

Syntax to write load template and logic using a single file.

def Methodname(request):  

       if request.method=="POST":

                 Write Business Code

                  return render(request,"appname/filename",{'key':output})

      return render(request,"appname/filename")

Now I am explaining the example of the SI Calculation App?

Step1st:-   Create APP using   python startapp siapp

Step2nd:-   go into and enter appname

Step3rd:-  go to project and create app urls

step4th:-  create under app and define method urls

from django.urls import path

from . import views





create view method under

from django.shortcuts import render

def index(request):

if request.method=="POST":

if request.POST.get("btnsubmit"):





return render(request,"siapp/index.html",{'key':'result is '+str(si),'p':p,'r':r,'t':t})


return render(request,"siapp/index.html",{'key':'','p':'','r':'','t':''})  

return render(request,"siapp/index.html")


create HTML file under the templates directory

<!DOCTYPE html>






<form action="" method="post">

{% csrf_token %}


<input type="text" name="txtp" placeholder="Enter Principal" value="{{p}}" />


<input type="text" name="txtr" placeholder="Enter Rate" value="{{r}}" />


<input type="text" name="txtt" placeholder="Enter Time" value="{{t}}" />


<input type="submit" name="btnsubmit" value="Calculate" />

<input type="submit" name="btnclear" value="Clean" />





Post a Comment

POST Answer of Questions and ASK to Doubt

Previous Post Next Post