Django
Django
Django Trainer
HarshTechnologies
What is Django??
Scope of Django
Architecture
Models/Views/Templates
quiestions
Web framework:
Software designed to develop web application
Eq: CakePHP, Spring, Django
Server Side: PHP/Java/Python/Ruby/….
Client Side: HTML/HTML5/CSS/Javascript/Jquery/…
History:
2003, Django Software Foundation
20% market share in silicon valley, Second place after ruby
Developed with python
High-level framework for rapid web development
Complete stack of tools
Data modelled with Python classes ,
Manipulate data, CRUD
Production-ready data admin interface, generated dynamically
Elegant system for mapping URLs to Python code
Generic views’ to handle common requests
Clean, powerful template language
Components for user authentication, form handling, caching . . .
Django is a MVT arcitecture , more than MVC. /salaries
Models: Alice 50000
Modelling the classes according to the data Bob 20000
Cathy 35000
Views
query the data according to the request
Template
Represent/Structured the data in to the HTML template.
django-admin.py startproject
<PROJECT_ROOT>
manage.py
<PROJECT_DIR>
__init__.py
settings.py
urls.py
wsgi.py
django-admin startproject <projectname>
cd <projectname>
django-admin startapp <appname>
cd <appname>
A model is the single, definitive source of information about your data.
Essential fields and,
Behavior of data your are storing.
When ever any changes in the model, you should make sure to run manage.py migrate –
run-syncdb
It is the list of database fields it defines.
Fields are specified by class attributes.
Field Types:
Column type which tells the database what kind of data to store.
AutoField/BigAutoField GenericIPAddressField
BigIntegerField PositiveIntegerField/PositiveSmallI
BinaryField ntegerField
BooleanField TextField
CharField TimeField
DateField URLField
DateField(auto_now=False, auto_now_a UUIDField
dd=False, **options)
DateTimeField
DecimalField
DurationField
EmailField
FileField
Null
Models.CharField(max_length=100,null=True)
Blank
models.CharField(max_length=100, blank=True, null=True)
Choices
Default
Help_text
Primary_key
unique
Relationships
Many to One Relationship
django.db.models.ForeignKey
MODEL – MANAGERS
Managers are intended to do TABLE-WIDE operations.
MODEL – INHERITANCE
AGGREGATION
Retrieve objects that are summerise by collection of objects.
Aggregate()
Annoted(num_books=Count(‘Book’))
Count(‘field value’)
Avg(‘price’)
Combining multiple annoted with and without distant.
Exact/iexact
Contains/icontains
In
id__in=[1, 3, 4] equivlent SELECT ... WHERE id IN (1, 3, 4);
Gt/gte/lt/lte
Startswith/istartswith
headline__startswith='Lennon‘ equivlent to SELECT ... WHERE headline LIKE 'Lennon%';
Endswith/iendswith
range
Date/year/month/day/quarter/time/hour/minute/second
isnull
Regex/iregex
How Django knows to UPDATE vs. INSERT
Updating attributes based on existing fields
Field CHOICE details get_FOO_display
Field date object get_next_by_FOO/get_previous_by_FOO
Queryset : iteration/slicing/reverse/annotate/exclude/filter/values/dates
A database query operations are provided to django model.
It is an interface that interacs with database.
Default is django.db.models.Manager
Using Managers.raw()
Manager.raw(raw_query, params=N
one, translations=None)
This method takes a raw SQL query
, executes it and return
django.db.models.query.RawQuery
Set.
Admin login
List_filter
List_display
Filters
Fieldsets
INlineEditing
Search_fields
date_hierarchy
ordering
ROOT_URLCONF
How Django processes a request
django.conf.urls.url()
Django runs through each URL pattern, in order, and stops at the first one that matches the
requested URL
Once one of the regexes matches, Django imports and calls the given view, which is a
simple Python function (or a class-based view). The view gets passed the following
arguments:
An instance of HttpRequest.
If the matched regular expression returned no named groups, then the matches from the
regular expression are provided as positional arguments.
The keyword arguments are made up of any named groups matched by the regular expression,
overridden by any arguments specified in the optional kwargs argument
to django.conf.urls.url().
Introduction to web framework
What is a server, HTTP Request and HTTP Response?
What is a web framework and web application?
Challenges in developing web application.
Django overview and installation
Models
The MVC Development Pattern
Defining Models using Python classes
Defining Model data fields
Initializing model using makemigrations
Running model initialization using migrate
Registering models in settings.py
Registering models with Admin site
Views and URLconfs
Understanding the view layer
Requesting a web page via URL
Rendering web page via view function
Render HTTPResponse to templates
Understanding context data and Python dictionary type
Forms
Form basics
GET and POST methods
Form validation
Rendering forms
ModelForm
Extending Templates
Creating a template libraryWriting custom template filterWriting custom templates
tagsRegistering the tagsSetting a variable in the contextWriting template loader
Testing in Django
Testing Django
Writing a unit test
Test ClientRequest
FactoryRunning Tests