Cheat-sheat project for Django Template system to showcase the configuration and dynamic HTML page rendering. For newcomers, Django is a high-level Python Web framework that encourages rapid development by reusing modules and libraries built by experienced programmers. Django Templates help us to build dynamic pages, reuse components and code faster web applications.
Links
- Django Templates - a comprehensive guide | AppSeed Docs
- Django Templates a popular related article published on Dev
How to build the project
Step #1 - Clone the sources
$ git clone https://github.com/app-generator/django-templates.git
$ cd django-templates
Step #2 - Create and activate a virtual environment
$ # Virtualenv modules installation (Unix based systems)
$ virtualenv env
$ source env/bin/activate
$
$ # Virtualenv modules installation (Windows based systems)
$ # virtualenv env
$ # .\env\Scripts\activate
Step #3 - Install Django
$ pip install django
The config
directory contains at least two relevant files:
config/settings.py
- global project settingsconfig/urls.py
- project routing rules
Sample_app
directory, created with python manage.py startapp
will serve the html
files from templates
directory. According to the product configuration, HTML pages are located in the templates
directory.
# Contents of config/settings.py
...
TEMPLATES_DIR = os.path.join(BASE_DIR, "templates") # <- Specify where the directory is located
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR], # <- Informs Django about it
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
...
index.html - showcases a simple file
On access the ROOT
of the app or simply index.html
a simple file is served from templates
directory.
variables.html - Display in the HTML page a simple variable sent by the Django backend
{{ variable }}
lists.html - iterate over a simple list with integers
<h4>The List:</h4>
<ul>
{% for i in list %}
<li>{{ i }}</li>
{% endfor %}
</ul>
Footer Component - a common footer is used by all sample pages
{% include "includes/footer.html" %}
Thanks for reading! Feel free to suggest more topics related to Django Templates using the issues tracker
.
- Django Templates - a free chapter from
The Django Book
- Django Templates - a really nice article provided by
GeeksForGeeks
platform
Django Templates - Open-source template project provided by AppSeed App Generator