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

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

How to create a form using the 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 urls.py to define method if not created.

urls.py

from django.urls import path
from . import views

urlpatterns=[
  path('',views.index,name='index'),
  path('home',views.index,name='index')
 
]

Step2nd:-  

Create templates/appname/filename to design template

create index.html

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

<center><h1>SI FORM </h1></center>
</header>
<section>
<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="" />
    <br><br>
    <input type="number" name="txtr" placeholder="Enter value for r" required="" />
    <br><br>
    <input type="number" name="txtt" placeholder="Enter value for t" required="" />
    <br><br>
    <input type="submit" name="btnsubmit" value="Calculate" />
  </form>
   <h1>Result of SI IS</h1>
    Amount is : {{p1}} <br>  Rate is : {{r1}}  <br> Time is : {{t1}} <br>
  {{res}}
</div>
  </section>
  <footer></footer>
</body>
</html>


3) Code of views.py


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

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post