Templates are used in Django to create HTML pages that can be dynamically generated based on data from a Django view. Here is a basic overview of how to use templates in Django:
1. Create a directory to store your templates. By default, Django looks for templates in a directory called templates within each app in your Django project.
2. Create an HTML file within your template directory. You can use any HTML code you want, but you can also include Django template tags and filters to insert dynamic content.
3. Create a Django view that renders your template. Within your view, you can pass data to your template using a dictionary or other data structure.
4. Include a URL pattern that maps to your view.
If you haven't install python and django you can start from here Installing python and django
For Quick Setup
Create a virtual environment
python -m venv env
Activate virtual environment
source env/bin/activate
Installing Django
pip install django
Creating your first Project
django-admin startproject projectname
Creating your first App
Change to project directory: "cd projectname"
django-admin startapp appname
you see app have its own file structure, hence it is independent of the project. To work with app we need to include it in project
projectname/projectname/settings.py
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"whitenoise.runserver_nostatic",
"django.contrib.sitemaps",
"appname", # <-- add this
]
Create Templates
Create a directory called templates within your Django app.
Create an HTML file called index.html within the templates directory:
<!DOCTYPE html>
<html>
<head>
<title>My Django App</title>
</head>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>
In this example, we use a Django template tag to insert the value of the name variable into the HTML output.
Create View
Create a view that renders your template:
projectname/appname/templates/index.html
from django.shortcuts import render
def index(request):
name = 'Bob'
return render(request, 'index.html', {'name': name})
In this example, we create a variable called name and pass it to the render() function as a dictionary value. This makes the name variable available to the index.html template.
Create Url
Include a URL pattern that maps to your view:
projectname/appname/urls.py
from django.urls import path
from .views import index
urlpatterns = [
path('', index, name='index'),
]
projectname/projectname/urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('appname.urls')), # <-- add this
]
In this example, we include a URL pattern that maps the root URL (/) to the index view.
With these steps in place, visiting the root URL of your Django app should display the index.html template with the dynamic content inserted based on the value of the name variable