Python Django Short Notes
1. Introduction to Django
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.
Key features:
- MVT architecture (Model-View-Template)
- Admin interface
- ORM (Object-Relational Mapping)
- Security features (CSRF, XSS protection)
- Scalability and reusability
2. Creating a Django Project
To start a project:
$ django-admin startproject projectname
Project structure:
- [Link]: Command-line utility
- projectname/: Main project folder with [Link], [Link], [Link], [Link]
To run server:
$ python [Link] runserver
3. Creating a Django App
Apps are modular components of a Django project.
Create an app:
$ python [Link] startapp appname
Register it in [Link] -> INSTALLED_APPS
Basic files in an app:
- [Link]: Define database models
- [Link]: Define views (controller logic)
- [Link]: Define routing (if added)
- [Link]: Admin interface configuration
4. Models and ORM
Django uses models to define database schema in Python.
Example:
class Post([Link]):
title = [Link](max_length=100)
content = [Link]()
Migrations:
$ python [Link] makemigrations
$ python [Link] migrate