Django Templates
It’s easy to create awesome Python-based web pages using Templates in the Django Library. Would you like to create highly functional and manageable Html pages using Templates?

When making web pages with Django, you can use the Template method. This will make your work easier and help you design a more flexible and manageable website.
Index
Django Template View
First, let’s create a project for the Django Template view and an App within this project. We explained how to do this in our article Creating an App with Django. Now it is useful to go over it again.
First Create the Project Folder
You can create it manually in the project folder. Or you can create it manually in the Terminal section of VS Code.
Here I will answer the second question, how to create a Django Project and Application via Terminal.
cd Desktop
mkdir Template
cd Template
django-admin startproject Template
python manage.py startapp temp_app
TerminalVS Code Screenshot:

In the VS Code Explorer tab, if a view similar to the one above opens, you have created a project without any problems. Now let’s create a standalone urls.py file for temp_app.
To do this, right click on temp_app and click on the New File tab. Type urls.py as the file name and press enter. Then add the following lines of code in the newly created urls.py.
from django.urls import path
from . import views
urlpatterns = [
dj
path("",views.index,name="index")
]temp_app/urls.pyIn the urls.py file in the project folder, let’s add the second path line;
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("temp_app/", include("temp_app.urls"))
]urls.pyLet’s create a page view to test the code we’ve written so far. For this we will make some additions to temp_app/views.py.
from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
# Create your views here.
def index(request):
return HttpResponse("Main Page")
temp_app/views.pyAfter typing all these changes and saving the individual files, let’s run the Python Web server by typing the following command on the terminal screen.
python manage.py runserver
TerminalNow type http://localhost:8000/temp_app in your computer’s web browser application and access the page as below.

If you can see this page as a test screen, we can move on to the next stage.
Using Render Module in Template
Render, in its simplest form, can be seen as a request and a template definition. You may need an Html page for this. Let’s start by defining a folder for templates.
Creating a Templates Directory
Right-click on the Root folder of your project, click New Folder and create a folder called templates.

Under the “/templates” folder, click New Folder again and create another folder temp_app (it is important that it has the exact same name). Then right click on the temp_app folder, select New File and create a file called first.html.
Make sure that the final project folders are as follows.

Html Page Preparation in Templates Folder
Let’s prepare our web HTML page by typing the tags in first.html. As a shortcut, if you type “doc” on the page and TAB-TAB, the tags of an empty HTML template will be written automatically.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Main Page</title>
</head>
<body>
<h1>Hi WORLD!</h1>
<h2>First.html Test OK</h2>
</body>
</html>templates/first.htmlWe have created a simple Html page but we need to define it in views.py.
from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
# Create your views here.
def index(request):
return render(request,"temp_app/first.html")
temp_app/views.pyHow to Introduce the Templates Folder to Django
In order to call this html page from the browser, we need to define the templates folder in Django.
Defining in Settings.py (Workaround)
One of the ways to do this is to define it in the TEMPLATES line in the settings.py file.
Before this definition, it will be necessary to add the OS (Operating System) library at the beginning of settings.py. Because we will need to be able to define the user independent file path. Change the”DIRS” line as follows.
from pathlib import Path
import os
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [os.path.join(BASE_DIR,"templates/")],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],settings.pyMigrate Method (Recommended Method)
Stop the running Runserver with CTRL+C and in Terminal;
python manage.py migrateTerminalcommand. What this command does is to apply models and similar operations. You will get a screen like this when you run the command.

After this command, directories named __pycache__ and migrations will be created under the application folder.
In setting.py, let’s add the first line starting with temp_app below in INSTALLED_APPS.
INSTALLED_APPS = [
"temp_app.apps.TempAppConfig",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]settings.pyAfter adding this line, run the following command on the Terminal screen:
python manage.py makemigrations temp_appTerminalDelete the templates directory we created earlier and delete the change we made to the DIRS directory in settings.py.
Let’s add the following function in app/views:
from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
# Create your views here.
def index(request):
return render(request,"temp_app/first.html")
temp_app/views.py- Right click on the temp_app directory and create a directory called templates with New Folder.
- Name this directory temp_app, the same name as the application folder.
- Create a file named first.html in this directory.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>New Html Page</title>
</head>
<body>
<h1>Hi world again!</h1>
<h2>This page test is OK!</h2>
</body>
</html>temp_app/first.htmlSave everything you have done and run the server with the following code.
python manage.py runserverTerminalType http://localhost:8000/temp_app/ in the address line of your Web Browser and press enter. You will see our new web page:

No matter how big the project is, you can add and manage your applications and web pages with this model. This is the method we will use the most.
How to Design a Website with Python Using a Template?
In the light of all the information we have learned so far, let’s take some time for the scenario of how to display currency information in a web page. It will be an ideal example for making a web page using a template with Python.
First, let’s create a file called temp_app/currency.html.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>New Html Page</title>
</head>
<body>
<h1>Hi world again!</h1>
<h2>This page test is OK!</h2>
</body>
</html>temp_app/currency.htmlThen let’s make our necessary definition in views.py as follows.
from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
# Create your views here.
def index(request):
return render(request,"temp_app/first.html")
def currency(request):
return render(request,"temp_app/currency.html")
temp_app/views.pyThen, in temp_app/urls.py, add the following line with a comma:
from django.urls import path
from . import views
urlpatterns = [
path("",views.index,name="index"),
path("currency/",views.currency,name="currency")
]temp_app/urls.pyAs a result, when we type http://localhost:8000/temp_app/currency/ in the browser, the following screen will open.


![HP E-Series Switch Configuration [1]-Spanning Tree](https://www.omersahin.com.tr/wp-content/uploads/2015/07/ProCurve-front-1024x685.jpg)


