Django
Django is a high-level Python web framework known for its efficiency, scalability, and rapid development capabilities. It follows the Model-View-Controller (MVC) architectural pattern, facilitating the creation of robust and maintainable web applications. Django provides a comprehensive set of tools and libraries, including an ORM (Object-Relational Mapping) for database interaction, a templating engine for generating dynamic HTML, and a built-in administration interface.
Module 1: Introduction to Django
What is Django?
Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.
Setting Up a Django Project
Setting up a Django project involves installing Django, setting up a virtual environment, and creating a new Django project with Django's command-line utility.
$ pip install django $ django-admin startproject mysite
Module 2: Django Models and Databases
Creating Models
A model in Django is a special kind of object – it is saved in the database. A model is a Python class that subclasses django.db.models.Model in which each attribute represents a database field.
from django.db import models class Blog(models.Model): title = models.CharField(max_length=200) pub_date = models.DateTimeField('date published')
Django Admin
Django provides a built-in admin module which can be used to perform CRUD operations on the models.
from django.contrib import admin from .models import Blog admin.site.register(Blog)
Module 3: Django Views and URL Handling
Creating Views
A view function, or view for short, is a Python function that takes a web request and returns a web response. This response can be the HTML contents of a document, a redirect, a 404 error, an XML document, an image... or anything.
from django.http import HttpResponse def hello(request): return HttpResponse('Hello, World!')
URL Mapping
URLconfs have a hook to make URLs for models. This lets you wire a model’s methods into your URLconf without any redundancy.
from django.urls import path from . import views urlpatterns = [ path('hello/', views.hello), ]
Module 4: Templates and Static Files
Creating Templates
Django’s template language is designed to strike a balance between power and ease. It’s designed to feel comfortable to those used to working with HTML.
{% extends 'base_generic.html' %} {% block content %} <h2>Blog</h2> {% endblock %}
Managing Static Files
Django provides django.contrib.staticfiles to help you manage them. This app collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.
STATIC_URL = '/static/'
Module 5: Forms and Class-Based Views
Working with Forms
Handling forms is a complex business. Consider Django’s admin, where numerous items of data of several different types may need to be prepared for display in a form, rendered as HTML, edited using a convenient interface, returned to the server, validated and cleaned up, and then saved or passed on for further processing.
from django import forms class NameForm(forms.Form): your_name = forms.CharField(label='Your name', max_length=100)
Class-Based Views
Class-based views provide an alternative way to implement views as Python objects instead of functions. They are organized around the notion of components, standalone pieces of code that can be reused in a different context through inheritance.
from django.http import HttpResponse from django.views import View class MyView(View): def get(self, request): return HttpResponse('Hello, World!')