Introduction to Django - O'Reilly to Django Paper.pdfIntroduction to Django Creating a New Project The django-admin.py tool is used to create a directory and create “default” files for a new Django project

  • View
    214

  • Download
    2

Embed Size (px)

Transcript

  • One Copley Parkway, Suite 210 Morrisville, NC 27560 Phone: 919.463.0999 Fax: 866-229-3386www.opentechnologygroup.com

    Copyright 2010 Open Technology Group, Inc. All rights reserved.

    Introduction to Django

    Introduction to DjangoChander Ganesan

    OSCON 2010

    Page 1 of 84

  • One Copley Parkway, Suite 210 Morrisville, NC 27560 Phone: 919.463.0999 Fax: 866-229-3386www.opentechnologygroup.com

    Copyright 2010 Open Technology Group, Inc. All rights reserved.

    Introduction to Django

    Table of ContentsIntroduction............................................................................................4

    About Django.....................................................................................5Installing & Configuring Django Components......................................6

    Django Pre-Requisites.......................................................................7Downloading & Installing Django.....................................................8Choosing a Database..........................................................................9Creating a New Project....................................................................10Starting the Test Server....................................................................11Creating a New Application.............................................................12Django Contributed Apps................................................................13

    Building Django Applications..............................................................14Overview of A Typical Django Request..........................................15Setting up URL Patterns..................................................................16Django Views...................................................................................18More About HttpResponse..............................................................19

    Django Templates.................................................................................20Template Basics...............................................................................21Storing Template Files.....................................................................22Easy Rendering of Templates..........................................................23Media Files.......................................................................................24Media Settings.................................................................................25Basic Template Substitutions...........................................................26Template Filters...............................................................................27Template Tags..................................................................................28

    More on For Loops..........................................................................30RequestContext Processors..............................................................32Global Context Processors...............................................................34

    Database Models with Django..............................................................35About Database Models...................................................................36Configuring Django for Database Access........................................37Defining Django Models..................................................................39Understanding Model Fields & Options..........................................40Table Naming Conventions.............................................................42Creating A Django Model................................................................43Validating the Model.......................................................................46Generating & Running the SQL......................................................47Adding Data to the Model................................................................48Simple Data Retrieval Using a Model.............................................49Understanding QuerySets................................................................50Applying Filters...............................................................................51Specifying Field Lookups/Filters.....................................................52Lookup Types..................................................................................54Slicing QuerySets.............................................................................58Specifying Ordering in QuerySets...................................................59Common QuerySet Methods............................................................60Deleting Records..............................................................................62Managing Related Records..............................................................63Retrieving Related Records.............................................................66

    Page 2 of 84

  • One Copley Parkway, Suite 210 Morrisville, NC 27560 Phone: 919.463.0999 Fax: 866-229-3386www.opentechnologygroup.com

    Copyright 2010 Open Technology Group, Inc. All rights reserved.

    Introduction to Django

    Using Q Objects...............................................................................68Using the Django Admin Interface.......................................................69

    Enabling the Admin Interface..........................................................70Access Control with Sessions and Users..............................................72

    Cookies & Django............................................................................73The Django Session Framework......................................................75Sessions in Views............................................................................76

    The Django auth Application...........................................................77Using Authentication in Views........................................................78

    Login and Logout........................................................................79The Login Required Decorator........................................................80Django Contributed Apps................................................................81

    Generic Views......................................................................................82Simple Generic Views.....................................................................83

    Page 3 of 84

  • One Copley Parkway, Suite 210 Morrisville, NC 27560 Phone: 919.463.0999 Fax: 866-229-3386www.opentechnologygroup.com

    Copyright 2010 Open Technology Group, Inc. All rights reserved.

    Introduction to Django

    Introduction

    Page 4 of 84

  • One Copley Parkway, Suite 210 Morrisville, NC 27560 Phone: 919.463.0999 Fax: 866-229-3386www.opentechnologygroup.com

    Copyright 2010 Open Technology Group, Inc. All rights reserved.

    Introduction to Django

    About DjangoHigh level web framework

    Basic modules, classes, and tools to quickly develop and deploy web appsContains an ORM (Object-Relational Mapper) that allows for the use of standard Python language syntax when interfacing with a back-end database.

    Developer need not learn SQL, DDL, etc!Provides a template framework that allows HTML, XML, and other documents to be converted into templates, which can then be converted to output via a wide range of substitution techniques.Elegant URL support (fancy, beautiful URL's, no ?blah=blah, etc.)Multi-lingual

    Fast and easy to use, robust, flexible, and lots of contributed components available!Built in administrative interface to manage data models.Built-in authentication/access control componentsContributed geospatial support components (GeoDjango)Extensible!

    Page 5 of 84

  • One Copley Parkway, Suite 210 Morrisville, NC 27560 Phone: 919.463.0999 Fax: 866-229-3386www.opentechnologygroup.com

    Copyright 2010 Open Technology Group, Inc. All rights reserved.

    Introduction to Django

    Installing & Configuring Django Components

    Page 6 of 84

  • One Copley Parkway, Suite 210 Morrisville, NC 27560 Phone: 919.463.0999 Fax: 866-229-3386www.opentechnologygroup.com

    Copyright 2010 Open Technology Group, Inc. All rights reserved.

    Introduction to Django

    Django Pre-RequisitesPython 2.3 or higher

    No Python 3.0 support (yet)Database drivers for the database you wish to use

    PostgreSQL, Oracle, SQLite, MySQLWeb Server

    Django has a built-in web server for developmentHandles a single connection at a time.Not security-testedBy default listens only for local connections.

    Apache, IIS, etc. for deployment

    Page 7 of 84

  • One Copley Parkway, Suite 210 Morrisville, NC 27560 Phone: 919.463.0999 Fax: 866-229-3386www.opentechnologygroup.com

    Copyright 2010 Open Technology Group, Inc. All rights reserved.

    Introduction to Django

    Downloading & Installing DjangoDjango can be downloaded from http://www.djangoproject.com/download Unpack and install Django

    Unpack the sources and run the command below from the Django source directory as the root user:python setup.py install

    This command works the same on Windows as it does in the Linux environment, you just need to make sure you call the correct interpreter.

    Page 8 of 84

    import djangoprint django.VERSIONSample code to verify that Django is installed correctly.

    http://www.djangoproject.com/download

  • One Copley Parkway, Suite 210 Morrisville, NC 27560 Phone: 919.463.0999 Fax: 866-229-3386www.opentechnologygroup.com

    Copyright 2010 Open Technology Group, Inc. All rights reserved.

    Introduction to Django

    Choosing a DatabaseCurrent version of Django provide support