Template is a text file that define the structure of that file, such as html page. In Django Template are used to generate dynamic HTML by injecting data to placehoders.
- To Seprate Presntation Logic From business logic.
- Template allows us to dynamically generate content based on data pass from view.
- Template Variable {{variable}}
- Template Tags {{% %}} [Loop or Condition]
- First create one folder named "templates" in your app directory
- Inside templates folder create another folder "your app name"
- Put your html file inside that templates/"your app name" folder.
- For e.g index.html
- To show html page use following code:
# app views.py
from django.shortcuts import render
def index(request):
return render(request, 'htmlapp/index.html')We use DTL [Django Template Language].
- Suppose this is HTML file called index.html on htmlapp/index.html
<!DOCTYPE html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1>Welcome to {{webname}} Website</h1>
</body>
</html>- Now We need to pass title and webname for this. You can use following code in views.
def index(request):
context = {
'title': 'Bishworaj Poudel',
'webname': 'BRPS'
}
return render(request, 'htmlapp/index.html',context=context)For e.g we have list of 10 names. We need to display all name in html list.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{title}}</title>
</head>
<body>
<h1>Welcome to {{webname}} Website</h1>
<h2>My New Students</h2>
<li>
{% for name in names %}
<ul>{{name}}</ul>
{% endfor %}
</li>
</body>
</html>def index(request):
context = {
'title': 'Bishworaj Poudel',
'webname': 'BRPS',
'names': ["Sita", "Ram", "Gita", "Hari"],
}
return render(request, 'htmlapp/index.html',context=context)It allows us to define base template and extend it on child templates. First create base.html on templates/"your app name" folder
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}{% endblock title %}</title>
</head>
<body>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<div class="container-fluid">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" href="index.html" >Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="service.html">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.html">Contact</a>
</li>
</ul>
</div>
</nav>
{% block content %}{% endblock content %}
</body>
</html>index.html code
{% extends "htmlapp/base.html" %}
{% block title %}Homepage{% endblock title %}
{% block content %}
<h1>Welcome to {{webname}} Website</h1>
<h2>My New Students</h2>
<li>
{% for name in names %}
<ul>{{name}}</ul>
{% endfor %}
</li>
{% endblock content %}service.html code
{% extends "htmlapp/base.html" %}
{% block title %}Service{% endblock title %}
{% block content %}
<h1>Welcome to Service</h1>
{% endblock content %}You can load css, js and images as static file in django. For that follow the step.
- Create static folder in your app directory.
- Inside your static folder create folder with appname
- Inside your folder create folder css and js
- Inside css folder place your css there and inside js folder place your js there.
- Use that css and js, or images in your project or html template.
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to Website</title>
<link rel="stylesheet" href="{% static 'brpapp/css/style.css' %}">
</head>
<body>
<h1>My Website</h1>
<script src="{% static 'brpapp/js/scripts.js' %}"></script>
</body>
</html>Place your images to folder static//images. Use following code in templates.
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to Website</title>
<link rel="stylesheet" href="{% static 'brpapp/css/style.css' %}">
</head>
<body>
<h1>My Website</h1>
<img src="{% static 'brpapp/images/brp.jpg' %}" alt="My Photo">
<script src="{% static 'brpapp/js/scripts.js' %}"></script>
</body>
</html>- {{value|capfirst}} : Capitalizes first letter of text.
- {{value|lower}}: Lower Case for text
- {{value|upper}}: Upper Case for text
- {{value|length}}: Find the length of text
- {{value|title}}: Convert it to title case
- {{value|wordcount}}: Convert it to title case