Unit 5: Frameworks notes, polytechnic 3rd semester python notes

5.1 Overview of Django

Django is a high-level, open-source Python web framework designed to simplify the creation of complex web applications. It encourages rapid development and clean, pragmatic design. Django comes with a lot of built-in features like an admin panel, ORM (Object-Relational Mapping), and authentication, reducing the need for developers to build common web functionalities from scratch.

Key Features of Django:

  • MVT (Model-View-Template) Architecture: Django follows the MVT pattern, where Models define data structure, Views control the logic, and Templates control the layout and presentation.
  • Built-in Admin Interface: Automatically generated based on your models, the admin panel allows for easy management of application data.
  • Scalability: Django is suitable for handling high-traffic websites.
  • Security: Django includes protection against common vulnerabilities like SQL injection, cross-site scripting, and cross-site request forgery.
  • ORM: The built-in ORM allows developers to work with databases in an object-oriented way, abstracting SQL queries.

5.2 Django Design Philosophy

Django’s design philosophy is built around several guiding principles:

  1. Don’t Repeat Yourself (DRY): Django aims to reduce repetition by using reusable components and abstractions. Once a functionality is written, it can be reused across the application.

  2. Loose Coupling: The different layers of a Django application (such as database models, views, and templates) are loosely coupled, allowing flexibility and separation of concerns.

  3. Explicit over Implicit: Django encourages clear, explicit code, prioritizing clarity over clever shortcuts.

  4. Rapid Development: Django helps developers build applications quickly with its tools, libraries, and shortcuts, reducing time spent on repetitive tasks.

  5. Clean Design: Django promotes maintaining clean, well-structured code that is easy to read and maintain, ensuring that your application remains manageable as it grows.


5.3 Creating a Simple Django Project

To create a Django project, follow these steps:

  1. Install Django: First, install Django using pip.

    bash

    pip install django
  2. Create a New Project: Use Django's command-line tool to start a new project.

    bash

    django-admin startproject myproject
  3. Start the Development Server: Navigate to the project directory and run the development server.

    bash

    cd myproject python manage.py runserver
  4. Create an App: Inside your project, you can create apps to structure different functionalities of your web application. To create a new app:

    bash

    python manage.py startapp myapp
  5. Define Models: In the models.py file of your app, define data models.

    python

    from django.db import models class BlogPost(models.Model): title = models.CharField(max_length=200) content = models.TextField() published_date = models.DateTimeField()
  6. Migrate the Database: After defining your models, apply the migrations to sync the database.

    bash

    python manage.py makemigrations python manage.py migrate
  7. Create Views: In views.py, define the logic to handle web requests.

    python

    from django.shortcuts import render from .models import BlogPost def blog_list(request): posts = BlogPost.objects.all() return render(request, 'blog_list.html', {'posts': posts})
  8. Create Templates: In your templates folder, create HTML files to render views.

    html

    <!-- blog_list.html --> <h1>Blog Posts</h1> <ul> {% for post in posts %} <li>{{ post.title }} - {{ post.published_date }}</li> {% endfor %} </ul>

This basic structure of a Django project demonstrates how you can build a web application using models, views, and templates.


5.4 Django App Life Cycle

The life cycle of a Django app involves several stages, from development to deployment:

  1. Development:

    • The project starts with creating models to define the structure of the database.
    • Views are developed to handle user input and return appropriate responses.
    • Templates are created for rendering the content in the browser.
  2. URL Routing:

    • Django uses a URL dispatcher to match requested URLs with views. URL patterns are defined in the urls.py file of the project and individual apps.
  3. Database Migration:

    • Migrations are used to create and update database tables according to the models defined in your app.
  4. Testing:

    • Django provides a testing framework to ensure the functionality works as expected before deployment.
  5. Deployment:

    • The project is deployed to production, usually on a web server like Apache, Nginx, or via cloud platforms.
    • Tools like Gunicorn or uWSGI can be used to serve the Django application in a production environment.
  6. Maintenance:

    • As the application evolves, the database schema, models, and views are updated. Django ensures smooth transitions through migration tools and well-structured workflows.

Summary:

  • Django is a powerful Python web framework designed for rapid development, security, and scalability.
  • The design philosophy of Django is centered on DRY, loose coupling, and clear code.
  • Creating a simple Django project involves setting up the project, creating apps, defining models, writing views, and rendering templates.
  • The Django app life cycle covers stages from development and database migration to deployment and maintenance.

Post a Comment

0 Comments