Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, 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  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 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

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

Complete Code of Templates:-

Code of 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

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

Previous Post Next Post