Web Development with Django
Santiago Dueñas
sduenas@[Link]
October 7th, 2010
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 1
Introduction to Django
Web development framework
Provides a set of resources for creating complex web
applications in an easy way
Development based on Python programming language and
the Model-View-Controller pattern (MVC)
Open Source Project (BSD License)
[Link]
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 2
Model-View-Controller Pattern (MVC) (i)
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 3
Model-View-Controller Pattern (MVC) (ii)
Model: Representation of the data on which the
application operates (raw data)
View: User interface
Controller: Manages the users requests and acts as
exchanger between the model and the views.
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 4
Starting a Project
A project is a collection of settings for an instance of
Django, including database configuration,
Django-specific-options, and application-specific settings
(Django Book )
An application is a portable set of Django functionality,
usually including models and views, that lives together in a
single Python package.
Initial commands
[Link] startproject <PROJECT>
[Link] startapp <APP>
[Link] runserver [DOMAIN]
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 5
Introduction to DB Model
Data access layer
Supports several database storage engines (MySQL,
PostgreSQL, SQLite, Oracle)
Access based on an ORM (Object Relational Mapper)
Suitable for object-programming
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 6
Database Configuration
[Link]
DATABASE_ENGINE = ""
DATABASE_NAME = ""
DATABASE_USER = ""
DATABASE_PASSWORD = ""
DATABASE_HOST = ""
DATABASE_PORT = ""
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 7
Useful DB commands
Validate model
python [Link] validate
Generate SQL
python [Link] sqlall <app>
Synchronize Model
python [Link] syncdb
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 8
Defining the Model
All classes inherit from [Link]
from [Link] import models
class Author([Link]):
name = [Link](max_length=50)
email = [Link]()
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 9
Model Fields
- AutoField
- CharField
- TextField
- IntegerField
- FloatField
- DateTimeField
- BooleanField
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 10
Model Relationships
ForeignKey(othermodel) -> 1-N
[Link] = b
OneToOneField(othermodel) -> 1-1 (FK with unique=True)
[Link] = b
ManyToManyField(othermodel) -> N-N
[Link](b)
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 11
Model Field options
null=Boolean
default=Type
primary_key=Boolean
unique=Boolean
unique_for_date=Boolean
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 12
Dealing with Model objects
Creating
a = Author(name=’sduenas’, ...)
[Link]()
Updating attributes
[Link] = ’carlosgc’
[Link]()
Deleting
[Link]()
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 13
Searching
Basic search
[Link]()
[Link]()
Filters
[Link](name="")
[Link](name="Jesus", email__contains="[Link]"
[Link].order_by("name", "-email")
[Link](name="").order_by("name")
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 14
Introduction to URLconfs
Regular Expressions
Map URL patterns to views
from [Link] import *
urlpatterns = patterns("",
(r"^mysite/", include("[Link]")),
(r"^admin/", include("[Link]")),
)
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 15
Regular Expressions
Symbol Meaning
. (dot) any char
* (dot) 0 or more previous chars
+ (dot) 1 or more previous chars
? (dot) 0 or 1 previous char
(?P<p>) group and parameter
[] set of chars
min,max range
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 16
URLconf Example
urlpatterns = patterns("",
(r"^id/(?P<name>[A-Za-z\-]+)$", "[Link]"),
(r"^id/(?P<name>[A-Za-z\-]+)/auth/(?P<type>[a-z]+)$",
"[Link]"),
(r"^/num/(?P<num>d{2})$", "[Link]"),
(r"(?P<path>.*)$", "[Link]",
{"document_root": "/var/www/myproject/"},
)
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 17
Views
Render template
render_to_response(<template>, <dict>)
HttpResponse
HttpResponse(response, mimetype=<type>)
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 18
Templates
Produce a dynamic text output requested by the user
Composed by text, variables ({{ var }}) and tags ({% tag %
})
Context variables are passed from the view dictionary
Template and Context objects is also available
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 19
Tags (i)
if/else
{% if found %}
<p>Found</p>
{% else %}
<p>Not found</p>
{% endif %}
for
{% for day in week %}
<p>{{day}}</p>
{% endfor %}
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 20
Tags (ii)
include
{% include "[Link]" %}
block
{% block title %}
<h1>Title</h1>
{% endblock %}
extends
{% extends "[Link]" %}
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 21
More on Django
Administration Site
Forms
Template Engine Extensions
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 22
References
Django Book [Link]
Django Documentation
[Link]
logo
Santiago Dueñas sduenas@[Link] - October 7th, 2010 Web Development with Django 23