A Django template is a text document like HTML added some jinja syntax.
The syntax of the Django template language involves four constructs: {{ }}
or {% %}
.
In this shot, we are gonna be looking at Django comments, or how to use Django comment tags in an HTML file. A comment is mostly used by the developer to remember a line or to state what a particular line of code does.
Comments do not display on the home screen and are merely lines that can only be seen when looking at the source code, not from the browser but by downloading/cloning the main source code example from GitHub.
An example of comments is shown below:
{% comment "things to remember" %} remember to input data {% endcomment %}
Let’s start with installation.
pip install pipenv
pipenv shell
pipenv install django
django-admin startproject DjangoCommentTag ./
python manage.py startapp codebase
python manage.py migrate
python manage.py runserver
Go to settings.py and add the following.
# Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'codebase', ]
In views.py, add the following:
from django.shortcuts import render def home(request): life = 'it is just my passion to code out the world' context = { 'life': life } return render(request, 'app/home.html', context)
In the codebase app, create a folder and name it templates.
Inside the templates folder, create another folder and name it app. Then, inside the app folder, create an home.html file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1> History</h1> {% comment "things to remember" %} <p>remember to input data here</p> {% endcomment %} <p>{{life}}</p> </body> </html>
In the urls.py file in the DjangoCommentTag folder, add the following.
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), # including the codebase urls.py file path('', include('codebase.urls')) ]
In the codebase folder, create a file and name it urls.py
from django.urls import path #importing home function from views.py from .views import home urlpatterns = [ path('', home, name='home'), ]
Run the following commands:
python manage.py makemigrations
python manage.py migrate
Run the command below:
python manage.py runserver
Then, go to http://127.0.0.1:8000 to access the homepage.
RELATED TAGS
CONTRIBUTOR
View all Courses