A Django template is a text document or Python string marked using the Django template language. Some builds are recognized and interpreted by the template engine. The main ones are variables and tags.
Templates are provided with context. Rendering replaces a variable with its value, which is searched in context and executes a tag. Everything is output as is.
Base Template
Every application you write in Django consists of Python packages that follow certain conventions. Django comes with a utility that automatically generates the basic directory structure of the application, so you can focus on writing code instead of creating directories.
To create a new page, make sure you’re in the same directory as manage.py and type this command:
python manage.py startapp mypage
That will create a directory mypage.
Open the file mypage/views.py and put the following Python code in it:
from django.views.generic import TemplateView
from django.conf import settings
from _keenthemes.__init__ import KTLayout
MypageView(TemplateView):
template_name = 'template-path.html'
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# A function to init the global layout. It is defined in _keenthemes/__init__.py file
context = KTLayout.init(context)
return context
In the mypage/urls.py file include the following code:
The next step is to point the root URLconf at the mypage.urls module. In _keenthemes/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list, so you have:
A template is a text file.
The template contains variables, which are replaced by values when the template is evaluated, and tags, which control the logic of the template.
Below is a minimum template that illustrates some of the basics.
{% include './partials/aside/_base.html' %}
Pages
This is a minimal views.py file to import with Metronic Django base layout file.
from django.views.generic import TemplateView
from _keenthemes.__init__ import KTLayout
class DashboardsView(TemplateView):
# Default template file
# Refer to dashboards/urls.py file for more pages and template files
template_name = 'dashboards/dashboard-1.html'
# Predefined function
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# A function to init the global layout. It is defined in _keenthemes/__init__.py file
context = KTLayout.init(context)
return context
For more detailed information, check on the link below to see the Django documentation page: Creating the Polls app