
WHAT IS DJANGO?
- Django is a high level Python Web framework that encourages rapid development and clean, pragmatic design.
- It is a free and open source web application framework.
WHERE DID DJANGO COME FROM?
- It is developed from a online news operation.
- It’s primary goal is to “ease the creation of complex, database-driven websites”
- It lets you built high performing, elegant web applications quickly.
- Written in Python, 2005.
COOL DJANGO FEATURES:
- Adheres to the DRY principle.
- Emphasizes re-usability.
- Focuses on automating as much as possible.
- Provides an optional admin account.
- Object Relational Mapper (ORM)
- Automatic admin interface.
- Elegant URL design.
- Cache system.
- Internationalization.
COMPANIES WHICH USE DJANGO:
- Mozilla Foundation
- The Washington Foundation
- Disqus
- Public Broadcasting Services(PBS)
- OpenStack
DJANGO’S ORM:
Object relational mapping:
A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you are storing. Generally, each model maps to a single database table.
The basics:
- Each model is a Python class that subclasses django.db.models.Model
- Each attribute of the model represents a database field.
- With all of this, Django gives you an automatically generated database-access API.
from django.db import models
class Person(models.Model):
first_name=models.CharField(max_length=30)
last_name=models.CharField(max_length=30)
Equivalent table created in SQL:
CREATE TABLE myapp_person(
“id” seriel NOT NULL PRIMARY KEY,
“first_name” varchar(30) NOT NULL,
“last_name” varchar(30) NOT NULL
);
DJANGO’S ADMIN INTERFACE:
As simple as configuring your settings, and creating a user:
- Python manage .py createsuperuser
- Fill in user name and password.
- And then go to your _domain/admin.
DJANGO’S TEMPLATE LANGUAGE:
1. Templates
- Simply a text file
- Contains variables and tags.
- base.html
2. Variables
- {{variable}}
- Evaluates variable and replaces with replaces with result.
- Alphanumeric characters and underscore only.
- Dot notation to access attributes of a variable.
3. Filters
- Modify variables for display by using filters.
- Apply filters with a pipe (|), filters can be chained.
- Examples: {{list|join:”,”}} , {{value|length}} , {{value|striptags}} , {{name|lower}}
4. Tags
- Create text in the output, load external information
- Control flow, loops or logic.
- for, if elif else, block and extends.
5. Comments
6. Template Inheritance
- {% extends “base.html” %}
- {% block content %} Content here {% endblock %}
DJANGO VS FLASK:
- Not a fair comparison, depends on what you want to do with either of them.
- Flask is simpler, Django more complicated.
- Flask has less built-in stuff, Django has more features ans is more powerful and robust.
- Flask is pure Python, Django has it’s own ‘pseudo’ language.
- Flask is easy to get started. Django requires a little learning before starting.
TIP OF THE DAY:
- Django is awesome!!!
- ORM can help save time and makes CRUD operations a lot nicer and easier.
- Admin account makes interacting with the page content super simple.
- Template language makes working with models and page content easier and more integrated, template tags and logic make HTML less ugly.
- Great documentation, easy to learn, and makes web development faster and easier for most people.
