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 a subfolder using the 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>
  <form action="addlogic" method="get">
    <input type="text" name="txtnum1" placeholder="Enter First Number" />
     <input type="text" name="txtnum2" placeholder="Enter Second Number" />
     <input type="submit" name="btnsubmit" value="Addition" />



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


POST Answer of Questions and ASK to Doubt

Post a Comment (0)