User:Niraj/Teaching-27

From ICTED-WIKI
Jump to navigation Jump to search

Teaching lesson plan 27 Subject: Python programming

Date: 14 Feb 2024

Time: 60 minutes

Period: 3rd

Teaching Item: Introduction to Django Templates

Class: Bachelor

Objective:

Students will learn the fundamentals of Django templates, including syntax, template inheritance, context variables, loops, conditionals, and template tags.

Materials Needed:

  • Python installed (preferably Python 3.x)
  • Text editor or integrated development environment (IDE) such as VSCode, PyCharm, or Sublime Text
  • Django framework installed (pip install django)
  • Projector

1. Introduction to Django Templates (15 mins)

  • Overview of Django templates:
    • Django templates are HTML files with embedded Django template language.
    • Templates allow for dynamic rendering of data passed from views.
  • Discuss the importance of templates in separating presentation logic from application logic.

2. Template Syntax (20 mins)

  • Basic template syntax:
    • Use double curly braces ({{ variable }}) to output context variables.
    • Use {% %} tags for template tags and control structures.
  • Template variables:
    • Accessing and displaying variables passed from views.
  • Template tags:
    • Using {% if %}, {% for %}, {% include %}, {% extends %}, etc.

3. Template Inheritance (20 mins)

  • Template inheritance:
    • Create a base template with common layout and structure.
    • Extend the base template in child templates to override specific blocks.
    • Use {% block %} tags to define sections that can be overridden.
  • Example: Creating a base template and extending it in child templates.

4. Context Variables and Context Processors (20 mins)

  • Passing context variables to templates:
    • Pass data from views to templates using the render() function.
    • Access context variables in templates using template variables.
  • Context processors:
    • Define custom context processors to add common data to all templates.
    • Register context processors in the Django settings.

5. Template Filters (20 mins)

  • Using template filters:
    • Modify variable output using built-in and custom filters.
    • Apply filters to format dates, numbers, strings, etc.
    • Chain multiple filters to transform data.
  • Example: Using built-in filters like date, title, truncatewords, etc.

6. Template Tags and Control Structures (20 mins)

  • Template tags for control structures:
    • Use {% if %}, {% for %}, {% with %}, {% url %}, etc., for conditional logic and looping.
    • Include external templates using {% include %}.
  • Advanced template tags:
    • {% csrf_token %} for CSRF protection.
    • {% static %} for serving static files.
    • {% trans %} for translation.

7. Exercise: Create and Render Templates (20 mins)

  • Provide a hands-on exercise where students create Django templates and render them using views.
  • Task:
    • Create multiple HTML templates with common layout and structure.
    • Use template inheritance to extend a base template in child templates.
    • Pass context variables from views to templates and display them dynamically.

8. Conclusion (10 mins)

  • Recap the key points covered in the lesson:
    • Django templates allow for dynamic rendering of HTML content using template language.
    • Template inheritance enables code reuse and modularization of templates.
    • Context variables passed from views provide dynamic data to templates.
    • Template filters and tags enhance the functionality and flexibility of templates.
  • Encourage students to continue exploring Django template language and practice creating templates for their projects.