Django Templates
×


Django Templates

512

Introduction to Django Templates

Django Templates provide a powerful tool to separate your application's logic from its presentation. They allow you to create dynamic HTML pages by combining Python data from your views with clean, maintainable template files. In this guide, you'll learn how Django's templating system works and how to use it effectively.

Setting Up Templates Directory

By default, Django looks for templates inside each app’s templates/ folder. To extend this, you can add a global directory. In your project’s settings.py, configure:


TEMPLATES = [
  {
    ...
    'DIRS': [BASE_DIR / 'templates'],
    ...
  },
]

Then, create the folder templates/ at project root and add HTML files inside.

Rendering Templates in Views

To generate a page, import Django’s render function in your view and pass it the context you want to display. For example:


from django.shortcuts import render

def home(request):
    data = {
        'title': 'Welcome',
        'items': ['Apples', 'Oranges', 'Bananas']
    }
    return render(request, 'home.html', data)

Template Language Basics

Django Templates use curly brace syntax to inject variables and logic into HTML. For instance:


<h1>{{ title }}</h1>
<ul>
{% for item in items %}
  <li>{{ item }}</li>
{% endfor %}
</ul>

You can also use filters:


<p>{{ title|lower }}</p>

Template Inheritance

Template inheritance helps you avoid repetition by creating a base layout. For example, base.html may contain a header and footer:


<!-- base.html -->
<html>
{% block title %}My Site{% endblock %}

  <header>...</header>
  <main>{% block content %}{% endblock %}
  <footer>...</footer>


Then, a child template can extend it:


<!-- home.html -->
{% extends "base.html" %}
{% block title %}Home Page{% endblock %}
{% block content %}
  <p>Welcome to my home page!</p>
{% endblock %}

Using Template Tags

Django offers built-in tags like if, for, url, and more. For example:


{% if user.is_authenticated %}
  <p>Welcome, {{ user.username }}!</p>
{% else %}
  <p><a href="{% url 'login' %}">Login</a></p>
{% endif %}

Custom Template Filters and Tags

You can enhance templates by creating custom filters or tags. To do this, create a templatetags folder in your app and register your filters there. This allows for reusable formatting logic across projects.

Best Practices for Django Templates

  • Keep templates free of business logic—views should handle data processing.
  • Use template inheritance to maintain consistent layouts.
  • Organize template folders by function or app.
  • Leverage built-in filters and tags to keep templates clean.

Conclusion

Django's templating engine is a flexible and efficient way to build dynamic web pages. By separating HTML from Python logic, using inheritance, and leveraging built-in tags and filters, you can create clean, maintainable, and reusable front-end components. Mastering templates is essential for professional Django development.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat