Python and model-template-views based Django is a free and open-source web framework. Once you have set up Django, you are all set to go.
After creating the Django project my_project
, create an app in Django named my_app
.
Django looks for HTML files in the templates folder. A templates folder contains the static HTML and static content needed to display the dynamic content. So, create a folder named Templates in my_app
folder.
In the templates directory, create the index file containing the static content you want to display.
<!DOCTYPE html><html lang="en"><head><title>Home Page</title></head><body><h1>Home Page</h1><p>This page is rendered as a Django Template</p><h3>Happy Learning :)</h3></body></html>
App listing: Add the name of the newly created app my_app
to the INSTALLED_APPS
list in settings.py
.
# Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','my_app']
Templates settings: Examine the TEMPLATES
settings in the settings.py
file, which defines how Django will load and render templates, and contain the configurations for the template engines.
TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [],'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',],},},]
BACKEND:
The location of the Django Template engine class by default.
DIRS:
A list of directories in a specific sequence where the search engine should seek source files.
APP_DIRS:
If it is true, the engine will search installed programs for templates.
OPTIONS:
Options for the backend are found here.
There are no adjustments that need to be made to these settings. The Django template engine will take care of other things because we have added our app to the INSTALLED_APPS list.
Creating a view: In my_app/views.py,
create a view for the template.
from django.shortcuts import render# Create your views here.def index(request):return render(request, 'index.html')
The HTML page is rendered via the index()
method. The render helper method from the Django.shortcuts module enables this.
In this case, render()
function produces a HttpResponse object with the displayed text after accepting the request object, and the template_name as parameters.
Setting up the URL: In my_project/urls.py
, set up the URL for the view we've created for the template.
"""my_project URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/3.2/topics/http/urls/Examples:Function views1. Add an import: from my_app import views2. Add a URL to urlpatterns: path('', views.home, name='home')Class-based views1. Add an import: from other_app.views import Home2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))"""from django.contrib import adminfrom django.urls import pathfrom my_app.views import indexurlpatterns = [path('', index, name='index'),]
The directory structure will look like this.
project_name/├── manage.py├── my_project/│ ├── __init__.py│ ├── settings.py│ ├── urls.py│ ├── asgi.py│ └── wsgi.py└── your_app/├── __init__.py├── views.py├── apps.py└──templates/└── index.html
Now that the files have been all setup and the HTML file has been created, you are all set to go.
#!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import os import sys def main(): """Run administrative tasks.""" os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc execute_from_command_line(sys.argv) if __name__ == '__main__': main()
Free Resources