How to create form using POST Method


POST is the most secure and reliable method to send data from client machines to server machines hence we should always use the POST method to create form elements.

Django provides the CSRF option to send data using the POST method.

CSRF means Cross-site request forgery. it will protect invalid requests under the webserver to check the security token.

If the security token is valid then the server will process requests otherwise deny the request.

Now we are creating a calculation of simple interest using the POST method.

Step1st:-  Create an app if not exist, create to define method if not created.

from django.urls import path
from . import views



Create templates/appname/filename to design template

create index.html

<!DOCTYPE html>
<link href="/static/style.css"  type="text/css" rel="stylesheet" />

<center><h1>SI FORM </h1></center>
<div style="padding-top:50px;padding-left: 200px; ">
  <form action="home" method="post">
   {% csrf_token  %}
    <input type="number" name="txtp" placeholder="Enter value for p" required="" />
    <input type="number" name="txtr" placeholder="Enter value for r" required="" />
    <input type="number" name="txtt" placeholder="Enter value for t" required="" />
    <input type="submit" name="btnsubmit" value="Calculate" />
   <h1>Result of SI IS</h1>
    Amount is : {{p1}} <br>  Rate is : {{r1}}  <br> Time is : {{t1}} <br>

3) Code of

from django.shortcuts import render

def index(request):
if request.method=='POST' and request.POST['btnsubmit']:
  p = request.POST["txtp"]
  r = request.POST["txtr"]
  t = request.POST["txtt"]
  si = (float(p)*float(r)*float(t))/100
  return render(request,"siapp/index.html",{'res':si,'p1':p,'r1':r,'t1':t})
return render(request,"siapp/index.html")


Post a Comment


POST Answer of Questions and ASK to Doubt

Post a Comment (0)