Creating My First Django Application Part 3

Handling GET Request

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from pprint import pprint
def index(request):
return HttpResponse('404')
def addition(request,num1,num2):
text = """The method used in the request is <b>%s</b></br>
Path of <b>%s</b> is <b><u>%s</u></b> </br>"""%(str(request.method),str(request.method),str(request.path))

addition = int(num1) + int(num2)
return render(request,'get.html',{"data":text,"num1":num1,"num2":num2,"sum":addition})
<HTML>
<HEAD>
<TITLE>This is a Sample Django Application</TITLE>
<style type=”text/css”>
body
{
background-color: #fcfcfc;
}
.center-div
{
position: absolute;
margin: auto;
top: 0;
font-size:20px;
right: 0;
bottom: 0;
left: 0;
width: 400px;
height: 400px;
background-color: #ccc;
border-radius: 3px;
}
</style>
</HEAD>
<BODY BGCOLOR=”FFFFFF”>
<div class=”center-div”>{{data|safe}}
<br> The sum of {{num1}} and {{num2}} is {{sum}}</div>
</BODY>
</HTML>
This will be escaped: {{ data }}
This will not be escaped: {{ data|safe }}
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path(‘/’,views.index),
path(‘addition/<int:num1>/<int:num2>’,views.addition,name=”addition”),

]
URL with GET Parameters

Handling POST Request

from django.shortcuts import render
from django.http import HttpResponse

from myapp.forms import Addition #importing form Addition
def index(request):
return HttpResponse(‘404’)
def total1(request):
if request.method == "POST":
num1 = int(request.POST['num1'])
num2 = int(request.POST['num2'])
total = num1 + num2
return render(request,'post_data.html',{"sum":total,"num1":num1,"num2":num2})
else:
return render(request,'add_data.html')
<html>
<body>

<form name = "form" action = "{% url 'total' %}"
method = "POST" >{% csrf_token %}

<div style = "max-width:470px;">
<center>
<input type = "text" style = "margin-left:20%;"
placeholder = "" name = "num1" />
</center>
</div>

<br>

<div style = "max-width:470px;">
<center>
<input type = "text" style = "margin-left:20%;"
placeholder = "" name = "num2" />
</center>
</div>

<br>

<div style = "max-width:470px;">
<center>

<button style = "border:0px; background-color:#4285F4; margin-top:8%;
height:35px; width:80%;margin-left:19%;" type = "submit"
value = "Login" >
<strong>Login</strong>
</button>

</center>
</div>

</form>

</body>
</html>
<HTML><HEAD>
<TITLE>This is a Sample Django Application</TITLE>
<style type=”text/css”>
body
{
background-color: #fcfcfc;
}
.center-div
{
position: absolute;
margin: auto;
top: 0;
font-size:20px;
right: 0;
bottom: 0;
left: 0;
width: 400px;
height: 400px;
background-color: #ccc;
border-radius: 3px;
}
</style>
</HEAD>
<BODY BGCOLOR=”FFFFFF”>
<div class=”center-div”><br> The sum of {{num1}} and {{num2}} is {{sum}}</div>
</BODY>
</HTML>
from django.contrib import admin
from django.urls import path
from myapp import views
from django.views.generic import TemplateView
urlpatterns = [
path(‘/’,views.index),
path('total/',views.total1,name='total'), #when post request is received total view will handle it]
Form Template
Output of Post Request

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store