Render Template In Django Part4 Django Tutorials

Опубликовано: 09 Октябрь 2024
на канале: Interview Coder
249
5

Link:https://tutorialwithproject.com/rende...



Join :
  / jagwithyou  
  / jagwithyou  
https://github.com/jagwithyou


In the last part, we have created a hello world application, and there we have returned a simple h2 tag to the request. but in the real world there will be lots of lines present so it's not the right way to write all the HTML code inside the view so we can use render method to render the HTML page.
Updating views to render the template


For rendering a template we are going to use render method it is automatically imported while creating the app so we don't have to import that if it's not been imported then import it first and then, use it with the return statement like below.
django_basics/hello_world/views.py


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


def home(request):
return render(request, "hello_world.html")



This render function takes the request as the first argument and the HTML page path from the template folder in the second argument.


Now we will create the template(i.e the HTML page what the render function is rendering for.


In Django, every template should be present inside the templates folder and the render function will search for the template from there. First, create a templates folder inside the hello_world app and create your template there(in our case hello_world.html).
django_basics/hello_world/templates/hello_world.html




NOTE: You can access your hello world page on "/hello_world". In localhost:8000/hello_world






Complete Playlist:
https://tutorialwithproject.com/what-...
https://tutorialwithproject.com/creat...
https://tutorialwithproject.com/hello...
https://tutorialwithproject.com/rende...
https://tutorialwithproject.com/stati...
https://tutorialwithproject.com/creat...
https://tutorialwithproject.com/add-a...
https://tutorialwithproject.com/templ...
https://tutorialwithproject.com/addin...
https://tutorialwithproject.com/link-...






Thank You
Keep Learning & Keep Growing




#TWP #DjangoTutorials #LearnOnLockdown