User:Niraj/Teaching-28

From ICTED-WIKI
Jump to navigation Jump to search

Teaching lesson plan 28 Subject: Python programming

Date: 15 Feb 2024

Time: 60 minutes

Period: 3rd

Teaching Item: Introduction to Django URLs

Class: Bachelor

Objective:

Students will learn the basics of URL routing in Django, including defining URL patterns, mapping URLs to views, and organizing URL patterns in a Django project.

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 URL Routing (15 mins)

  • Overview of URL routing:
    • URL routing maps URLs to views in a Django application.
    • URLs are defined in the project's urls.py modules.
  • Discuss the importance of URL routing in web applications and navigation.

2. Creating URL Patterns (20 mins)

  • Defining URL patterns:
    • Create regular expressions (regex) patterns to match URLs.
    • Define URL patterns using the urlpatterns list.
    • Use the path() function for simple URL patterns.
  • Example: Define a simple URL pattern to map a URL to a view function.

3. Mapping URLs to Views (20 mins)

  • Connecting URLs to views:
    • Import view functions or classes into the urls.py module.
    • Use the path() function to map URLs to view functions or classes.
  • Example: Map a URL pattern to a view function defined in a Django app.

4. URL Namespaces and Naming URL Patterns (20 mins)

  • URL namespaces:
    • Organize URL patterns using namespaces for better organization.
    • Avoid naming conflicts between different apps.
  • Naming URL patterns:
    • Assign unique names to URL patterns using the name argument.
    • Use named URLs in templates for dynamic URL generation.
  • Example: Define URL namespaces and name URL patterns in a Django project.

5. Including URL Patterns (20 mins)

  • Including URLs from apps:
    • Organize URL patterns by including URLs from individual apps.
    • Use the include() function to include URLs from other modules.
  • Example: Include URLs from multiple apps into the project's main urls.py module.

6. Path Converters and Capturing URL Parameters (20 mins)

  • Using path converters:
    • Capture dynamic segments of URLs using path converters.
    • Extract parameters from URLs and pass them to view functions.
  • Example: Define URL patterns with path converters to capture dynamic segments.

7. Exercise: Define and Map URLs (20 mins)

  • Provide a hands-on exercise where students define URL patterns and map them to views in a Django project.
  • Task:
    • Define multiple URL patterns for different views in a Django app.
    • Map the URL patterns to corresponding view functions or classes.
    • Test the URLs by accessing them in a web browser and verify the routing.

8. Conclusion (10 mins)

  • Recap the key points covered in the lesson:
    • URL routing maps URLs to views in a Django application.
    • URL patterns are defined using regular expressions and the path() function.
    • URL namespaces and naming URL patterns improve organization and maintainability.
    • Including URLs from multiple apps allows for modularization and reusability.
    • Path converters capture dynamic segments of URLs for flexible routing.
  • Encourage students to practice defining and mapping URLs in their Django projects and explore advanced routing techniques.