Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

django
python
communitycreator

How to use comments in Django template tags

Njoku Ifeanyi Gerald

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 %}
example

Let’s start with installation.

Step 1

pip install pipenv
pipenv shell
pipenv install django

Step 2

django-admin startproject DjangoCommentTag ./
python manage.py startapp codebase

Step 3

python manage.py migrate
python manage.py runserver

Step 4

Go to settings.py and add the following.

settings.py


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'codebase',
    
    
]
settings.py

Step 5

In views.py, add the following:

views.py

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)
views.py

Step 6

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.

home.html

<!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>
home.html

Step 7

In the urls.py file in the DjangoCommentTag folder, add the following.

urls.py

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'))
]

Step 8

In the codebase folder, create a file and name it urls.py

urls.py

from django.urls import path
#importing home function from views.py
from .views import home


urlpatterns = [
    path('', home, name='home'),
]
urls.py

Step 9

Run the following commands:

python manage.py makemigrations

python manage.py migrate

Step 10

Run the command below:

python manage.py runserver

Then, go to http://127.0.0.1:8000 to access the homepage.

RELATED TAGS

django
python
communitycreator

CONTRIBUTOR

Njoku Ifeanyi Gerald
RELATED COURSES

View all Courses

Keep Exploring