DOC PREVIEW
CU-Boulder CSCI 5448 - Django

This preview shows page 1-2-3-20-21-40-41-42 out of 42 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Web framework for deadline driven perfectionists By Tanvi Shah o Open Source Web application framework in Python o Designed to handle two challenges hard deadlines Rigid requirements o To build web applications quickly and easily Tidbits Django was named after a gypsy jazz guitarist Django Reinhardt A quick view of Django DESIGN THE MODEL Describe the database layout in Python code INSTALL Run the following utility to create the database tables Manage py syncdb API IT CREATION API is created on fly No code generation is necessary ADMIN INTERFACE Automatically provides an admin interface No need to create backend interfaces to manage content DESIGN URLs Create a Python module called URLconf The code maps URLs WRITE VIEWS A View is responsible for returning a HttpResponse object or Http404 exception object WRITE VIEWS A view retrieves data according to some parameters loads templates and renders template with the data that is retrieved DESIGN TEMPLATES Django uses the template inheritance concept Django designs Loose coupling and tight cohesion Every piece of a Django system is independent of each other Use as much less code as possible Quick development DRY Don t Repeat Yourself Explicit better than implicit This follows from the core Python principle Let there be no magic until very necessary Consistency Consistency for both Python code and higher level code Model view controller web development architecture pattern of Duplication is Devil Change of one element in a system should not affect any other part of the system Data transformation and code generators are the key to DRY code Design pattern used for maintaining multiple views of same data Object is divided into three parts Model is used to maintain the data View is used to display the data Controller handles events affecting the model Multiple views and controllers can interact with the same model Framework Features Object Relational Mapper Automatic Admin Interface Data model defined in Python with a dynamic database access API SQL queries can be written if necessary Interface for adding and updating readily provided Elegant URL Design Designing clean URLs with no php or cgi extension Template System Cache System Uses the Template Language for separating the design content and Python code Uses Caching Frameworks for super performance Internalization Provides support for different languages FRAMEWORK FEATURES IN DETAIL Data source that contains fields and behaviors and it maps to a single database table defines a model from django db import models Class Person models Model first name models CharField max length 30 last name model CharField max length 30 CREATE TABLE myapp person id serial NOT NULL PRIMARY KEY first name varchar 30 NOT NULL Courtesy docs djangoprojects com last name varchar 30 NOT NULL Every model is a Python class that subclasses models first name and last name are fields of the model database columns myapp person database table automatically created Id field also automatically added This is the most important part of the model Specified by class attributes as shown in the example in the previous slide Field class type determines integer or varchar data type for the database column There are different field options like default null max length primary key choices and unique are provided Database relationships available in Django Many to one relationship using foreign keys Many to many relationships One to one relationships Explicit is better than implicit Keyword arguments or type of the field determine the behavior No system knowledge is require and less faulty Includes all relevant domain logic Encapsulating data and information which is stored in the model Follows Active Record design pattern It connects business objects and database tables which creates a domain model Persons lastName firstName numberOfDependents Insert Update getExemption isFlaggedForAudit getTaxableEarnings Active Record is used to put data access logic in the domain object which enables reading and writing data to and from the database A database API is automatically created after creating the data model It can create retrieve update and delete objects The main objectives of this API are SQL Efficiency Terse powerful syntax Option to drop into raw SQL easily Django provides two methods of executing raw SQL queries for p in Person objects raw 1 Manager raw SELECT FROM myapp person print p This query gives the following result 2 Execute Custom SQL directly John Smith Jane Jones def my custom sql from django db import connection transaction cursor connection cursor Data modifying operation commit required cursor execute UPDATE bar SET foo 1 WHERE baz s self baz transaction commit unless managed ADMIN INTERFACE URL DESIGN TEMPLATE SYSTEM CACHING SYSTEM VIEWS The automatic admin interface provided by Django looks like this Admin s login screen Admin s Index page URL design in Django follows these principles Do not tie URLs to Python function names Infinite flexibility Encourages best practices Definitive URLs Designing URLs requires URLconf Python module URL patterns simply map to Python callback functions views Makes patterns function available from django conf urls defaults import articles 2005 03 matches the third entry in the list urlpatterns patterns r articles 2003 news views special case 2003 r articles d 4 news views year archive r articles d 4 d 2 news views month archive r articles d 4 d 2 d news views article detail Courtesy docs djangoproject com Django calls the function News views month archive request 2005 03 Separate logic from presentation Discourage redundancy Be decoupled from HTML XML should not be used for template languages Assume designer competence Treat whitespace obviously Don t invent a programming language Safety and Security Extensibility Text file containing variables that are replaced with values and tags which control the logic of the template defines a template language extends base generic html block title section title endblock block content h1 section title h1 for story in story list h2 a href story get absolute url story headline upper a h2 p story tease truncatewords 100 p endfor endblock This template extends base html section title variable replaced by title of the section object Dot access attributes of a variable Designed to strike balance between power and ease Caching Framework Dynamic pages can be cached to reduce overhead Django offers different levels of caching Caching output of specific


View Full Document

CU-Boulder CSCI 5448 - Django

Documents in this Course
ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download Django
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Django and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Django 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?