Django - Let's make your Homepage, Templates ! (Connecting FrontEnd to BackEnd)

In this chapter, We're going to learn "Templates" !


main - Templates - "making new folder" , let's name "main"


main - templates - main - "creating html file" 


<html>
<head>
    <title>Jay's World</title>
</head>

<body>
    <p>Base Template</p>

</body>

</html>

I named that file as "base.html"


Making another file named "home.html"

{% extends 'main/base.html '%}

It means  you're going to use "base.html" at every single pages. In other words, you can save a lot of time to make same things that you'd like to show on the every webpage that you make such as menu bars.


and going to main- views.py 

from django.shortcuts import render
from django.http import HttpResponse
from .models import ToDoList, Item

# Create your views here.

def index(response, id):
    ls = ToDoList.objects.get(id=id)
    return render(response, "main/base.html", {})

def home(response):
    return render(response, "main/base.html", {})

render(response # parameter that lines up with, "main/base.html" # templates location, {} #open dictionary) 


main - urls.py 

Making homepage!

from django.urls import path

from . import views

urlpatterns = [
path("<int:id>", views.index, name="index"),
path("", views.home, name="home")
]



Now go to the server ! 


You see the home page name "Jay's world" and   Base template ! 



Now I typed   /1  at the end of the address and "Base Template" shows again... !


With this function, On the next chapter, YOU will learn more cool things ๐Ÿ˜™

Popular posts from this blog

[Python] Dictionary

[Visual Design 2/3]

[JavaScript] For loop , Function