Upload
votuyen
View
218
Download
0
Embed Size (px)
Citation preview
DjangoOpen Data NC Bo Warren Ben Parisi Austin Schneider Brian Bollerman
About● free and opensource web framework
○ BSD licenced● written in python
○ supports any platform that python supports● purpose: ease creation of database driven, complex websites
○ covers most aspects of web development (databases, views, controllers)
● Follows Don’t repeat yourself Principle○ “Every piece of knowledge must have a single, unambiguous,
authoritative representation within a system.”○ - The Pragmatic Programmer○ synchronized changes
History● began in 2003 at Lawrence Journal-World newspaper, Kansas
○ to help web developers create websites efficiently within a limited time period
● Initial release: 2005● named after guitarist Django Reinhardt● In 2008 Django Software Foundation founded and received 501(c)(3) non-
profit status○ gives out “awards” ○ foster community
Motivation● What does it fix?
○ 2 challenges:■ making data (stories, blog posts, etc) look presentable and
consistent■ intensive deadlines of a newsroom
○ writing straight html/css/javascript/php/sql■ lots of time■ complicated■ learning curve
Structure of the Framework
Source: http://www.erikmitchell.info/2010/09/28/using-django-to-create-complex-sites-simply/
URL Handling
•Idea: Create a python module that maps URL patterns (as
regular expressions) to Python functions (the desired views for
each URL)
URL Handling
•Django uses file urls.py and looks for variable “urlpatterns”
which is of datatype list
•Django runs through each URL pattern in order and stops at the
first one that matches.
URL Handling
•Once one of the regular expressions matches, Django imports
and calls the chosen view, which is a Python function, and passes
it an instance of an HttpRequest class (created by Django), as
well as the matched parts of the regular expression
Structure of the Framework
Structure of the Framework
View
● When a regular expression is matched in urls.py○ a function in views.py is
called.● The function returns a render
of an html page or a 404.
Templates● Text documents or python strings that are marked-up using
Django Template Language to generate text-based formats (HTML, XML, CSV, etc.)○ Block tags:
■ {% if is_logged_in %} logged in? {% else %} login {% endif %}
○ Variables:■ My dog’s name is {{ dogs_name }}.
Templates cont.
Templates cont.
Database Interaction● supported dbs
○ postgreSQL, mySQL, Sqlite, Oracle● doesn’t support non-relational
○ e.g. mongoDB● little/no sql needs to be written by developer● why no sql?
○ streamline● tables are python classes
○ used as an api, much easier○ to interact with db, all you have to do is import the classes
■ create new objects of type class, set and interact w/ standard methods
Demo: ● Create, Populate, & Query a DB
Sites using Django● https://www.pinterest.com/
● http://www.theonion.com/
● http://instagram.com/
● https://disqus.com/