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"

What is  Templates in Django?:-


The template is used to design the web page using Html, CSS, JS, Jquery, Bootstrap, Angular, React.

 it will be called under views.py  method. Django provides render() to load template in the application.


Step to create Template:-


1 Create templates folder in-app  and create subfolder using  current app name  then create .html file under it


2 Create Path on urls.py for the action method


3 write code to load template using the load method

    def index(request):
        return render(request,"appname/htmlfilename")

4 Create action method in views.py


   def  actionmethodname(request):
          var= request.GET["textfiledname"]
          return HttpResponse(var)


Complete Code of Templates:-




Code of Urls.py of app (App Url)

from Django.urls import path

from. import views

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



Code of Index.html to design form:-

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
  <form action="addlogic" method="get">
 
    <input type="text" name="txtnum1" placeholder="Enter First Number" />
    <br>
    <br>
     <input type="text" name="txtnum2" placeholder="Enter Second Number" />
     <br>
     <br>
     <input type="submit" name="btnsubmit" value="Addition" />

  </form>

</body>
</html>


Code of Views.py

from django.shortcuts import render

from django.http import HttpResponse

def index(request):
return render(request,"addapp/index.html")
def addlogic(request):
    a = request.GET["txtnum1"]
    b = request.GET["txtnum2"]
    c= int(a)+int(b)
    return HttpResponse("result is "+str(c))




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