Django addition, multiplication, substraction and division using multiple button's

0





views.py code

from django.shortcuts import render

def index(request):
if request.method=='POST' 
p = request.POST["txtp"]
r = request.POST["txtr"]
if request.POST['btnsubmit']=='+':
c=int(p)+int(r)
elif request.POST['btnsubmit']=='-':
c=int(p)-int(r)
elif request.POST['btnsubmit']=='*':
c=int(p)*int(r)
return render(request,"siapp/index.html",{'res':c})
return render(request,"siapp/index.html")


code of 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="" method="post">
  {% csrf_token  %}
    <input type="number" name="txtp" placeholder="Enter first number" required="" />
    <br><br>
    <input type="number" name="txtr" placeholder="Enter second number" required="" />
    <br>
     <br>
    <input type="submit" name="btnsubmit" value="+" />
     <br>
      <br>
    <input type="submit" name="btnsubmit" value="-" />
     <br>
      <br>
    <input type="submit" name="btnsubmit" value="*" />
  </form>
   {{res}}
</div>
  </section>
  <footer></footer>
</body>
</html>

code of urls.py

from django.urls import path
from . import views

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

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)