Django Templates

Django Templates

In Django , templates are crucial for creating the static parts of web pages. They serve as the foundation or skeleton on which the dynamic content is added. Templates make Django follow the MVT (Model-View-Template) architecture: models handle data, views handle user interactions, and templates handle the layout and structure of web pages.

Simply put, templates in Django are like blueprints for web pages. They define how the page should look and what static elements it should contain. When a user visits a web page, Django uses views to render these templates and fill in any dynamic content before sending the complete webpage to the user's browser.

  1. Create a Template Folder

    Create a folder named templates in your Django application directory if it doesn't already exist. This folder will contain all your HTML template files.

  2. Specify the Template Folder Location in settings.py

    Open your settings.py file located in your Django projects' directory. Find the TEMPLATES variable and ensure that the DIRS option includes the path to your templates folder. Example:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
    }
]
  1. Place the HTML File Inside the Templates Folder

    Create or move your HTML template file into the templates folder.

    Example: Create a file named about.html and homepage.html inside the templates folder.

  2. Edit the Views in views.py File

    After ensuring that the HTML files are in place, you'll edit the views.py file to define the view functions for rendering these HTML templates. This code snippet is from a Django views.py file and contains two view functions: homepage and about.

     from django.http import HttpResponse
     from django.shortcuts import render
    
     def homepage(request):
        return render(request, 'homepage.html')
    
     def about(request):
         return render(request, 'about.html')
    

    Homepage Function: This function handles requests to the homepage of a website. Inside the function, it uses the render() function from django.shortcuts to render an HTML template named 'homepage.html'.
    The render() function takes the request object and the name of the template file as arguments and returns an HTTP response with the rendered template. In this case, the function returns the rendered homepage.html template as the HTTP response.

    About Function: This function handles requests to the about page of the website. Similar to the homepage function, it takes a request object as its parameter.

In summary, these view functions render HTML templates (homepage.html and about.html) and return them as responses to requests for the homepage and about page of the website, respectively.

  1. Include the View in the urls.py File

    Open your urls.py file located in your Django application directory. The code snippet is from a Django urls.py file and contains URL patterns mapped to view functions using the path() function.

from django.urls import path
from django.contrib import admin
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('about/', views.about, name='about'),
    path('', views.homepage, name='homepage'),
]
  • About Page URL: path('about/', views.about, name='about'): This URL pattern maps requests to the 'about/' URL to the about view function defined in the views.py file. The name='about' parameter provides a name for this URL pattern, which can be used to reference it in templates or view functions.

  • Homepage URL: path('', views.homepage, name='homepage'): This URL pattern maps requests to the root URL ('') to the homepage view function defined in the views.py file. The name='homepage' parameter assigns the name 'homepage' to this URL pattern for easy reference.

  1. Reload the Server

    Open a terminal or command prompt and navigate to your Django projects' directory. Run the command python manage.py runserver to start the development server. Open a web browser, navigate to the specified URL, and verify that the webpages (homepage.html and about.html) are rendered correctly.

By following these steps, you should be able to render HTML templates in your Django application and view them in your web browser.