
Create a folder inside your project, where 'manage.py' file is located, with any name say 'templates'
Create a file inside templates folder with any name say 'home.html' write any text with html tags.
Open settings.py from your project folder: add import os.path and under TEMPLATES list, under DIRS, write:
'DIRS':[os.path.join(BASE_DIR,'templates')], or 'DIRS':['templates'],
Change your views.py with following code: add from django.shortcuts import render
views.py
from django.http import HttpResponse
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
def home(request):
return render(request, 'home.html',{'name':'Ankit'})
def home(request):
params={'name':'Ankit', 'courses':['Django','PHP','JSP','ASP']}
data={
'student': [
{'name':'student1', 'subject':'web design', 'age':20},
{'name':'student2', 'subject':'Programming', 'age':30}
]
}
return render(request,'home.html',params)
urls.py
from django.contrib import path
from django.urls import admin
from appname import views
urlpatterns = [
path('', views.home),
]
Get the value of name in html file in the jinja format
home.html
<h1>Hello {{name}}</h1>
{% for n in courses %}
<div>{{n}}</div>
{% endfor %}
{% for n in courses %}
<div>{{forloop.counter}} {{n}}</div>
{% endfor %}
{% for n in courses %}
<div>{{forloop.counter0} {{n}}</div>
{% endfor %}
{% for n in courses %}
<div>{{forloop.revcounter} {{n}}</div>
{% endfor %}
{% for n in courses %}
<div>{{forloop.revcounter0} {{n}}</div>
{% endfor %}
{% for n in courses %}
<div>{{forloop.first} {{n}}</div>
{% endfor %}
{% for n in courses %}
<div>{{forloop.last} {{n}}</div>
{% endfor %}
{% if data|length > 0 %}
{% for n in student %}
{% if n.age>25 %}
<tr>
<td>{{n.name}}</td>
<td>{{n.subject}}</td>
<td>{{n.age}}</td>
</tr>
{% endif %}
{% endfor %}
{% else %}
No Data Found
{% endif %}
forloop.counter - Display current iteration of the loop(start from 1)
forloop.counter0 - Display current iteration of the loop(start from 0)
forloop.revcounter - Display iteration in reverse order, end at indexed 1
forloop.revcounter0 - Display iteration in reverse order, end at indexed 0
forloop.first - Display true at the first time of iteration
forloop.last - Display true at the last time of iteration
Ad: