User:Niraj/Teaching-24

From ICTED-WIKI
Jump to navigation Jump to search

Teaching lesson plan 24 Subject: Python programming

Date: 11 Feb 2024

Time: 60 minutes

Period: 3rd

Teaching Item: Setting Up Your First Django Web Application

Class: Bachelor

Objective:

Students will learn how to set up a basic Django web application, understand the Django project structure, create views, templates, and URLs, and run the development server.

Materials Needed:

  • Python (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 (15 mins)

  • Overview of Django:
    • Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.
    • It follows the Model-View-Template (MVT) architectural pattern.
  • Discuss the advantages of using Django for web development.

2. Installing Django and Setting Up the Environment (15 mins)

  • Installing Django using pip: pip install django
  • Setting up a virtual environment to manage dependencies: python -m venv myenv
  • Activating the virtual environment:
    • On Windows: myenv\Scripts\activate
    • On macOS/Linux: source myenv/bin/activate
  • Creating a new Django project: django-admin startproject myproject

3. Exploring Django Project Structure (15 mins)

  • Understanding the project structure:
    • manage.py: Command-line utility for interacting with the project.
    • myproject/: Directory containing project settings and configurations.
    • myapp/: Directory for creating Django applications.
  • Discuss the purpose of each file and directory in a Django project.

4. Creating a Django Application (15 mins)

  • Generating a new Django app within the project: python manage.py startapp myapp
  • Creating views, templates, and URLs for the app:
    • Define views in views.py.
    • Create HTML templates in the templates directory.
    • Define URL patterns in urls.py.

5. Defining Views and Templates (20 mins)

  • Creating views:
    • Define view functions to handle HTTP requests and return responses.
    • Render HTML templates using the render() function.
  • Creating templates:
    • Create HTML templates using Django template language.
    • Use template tags and filters for dynamic content.

6. Configuring URLs and Routing (20 mins)

  • Configuring URL patterns:
    • Define URL patterns in the urls.py file of the app.
    • Map URL patterns to corresponding view functions.
    • Include app-specific URL patterns in the project's main urls.py.
  • Testing URL routing by accessing different views in the browser.

7. Running the Development Server (10 mins)

  • Start the Django development server: python manage.py runserver
  • Access the application in a web browser at http://127.0.0.1:8000/.
  • Test the functionality of the web application by navigating through different URLs and views.

8. Exercise: Create a Simple Web Page (20 mins)

  • Provide a hands-on exercise where students create a simple web page using Django.
  • Task:
    • Create a new Django app and define views, templates, and URLs to display a basic HTML page.
    • Add some static content and dynamic content using Django template language.
    • Test the web page by running the development server and accessing it in a browser.

9. Conclusion (10 mins)

  • Recap the key points covered in the lesson:
    • Setting up a Django project and app.
    • Understanding the project structure and the purpose of each component.
    • Creating views, templates, and URLs to build web applications.
    • Running the development server to test the application locally.
  • Encourage students to continue exploring Django's features and build more complex web applications.