Django
Django
Santiago Dueñas
sduenas@libresoft.es
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 1
Introduction to Django
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 2
Model-View-Controller Pattern (MVC) (i)
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 3
Model-View-Controller Pattern (MVC) (ii)
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 4
Starting a Project
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 5
Introduction to DB Model
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 6
Database Configuration
settings.py
DATABASE_ENGINE = ""
DATABASE_NAME = ""
DATABASE_USER = ""
DATABASE_PASSWORD = ""
DATABASE_HOST = ""
DATABASE_PORT = ""
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 7
Useful DB commands
Validate model
python manage.py validate
Generate SQL
python manage.py sqlall <app>
Synchronize Model
python manage.py syncdb
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 8
Defining the Model
class Author(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField()
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 9
Model Fields
- AutoField
- CharField
- TextField
- IntegerField
- FloatField
- DateTimeField
- BooleanField
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 10
Model Relationships
logo
Santiago Dueñas sduenas@libresoft.es - 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@libresoft.es - October 7th, 2010 Web Development with Django 12
Dealing with Model objects
Creating
a = Author(name=’sduenas’, ...)
a.save()
Updating attributes
a.name = ’carlosgc’
a.save()
Deleting
a.delete()
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 13
Searching
Basic search
Author.objects.all()
Author.objects.get()
Filters
Author.objects.filter(name="")
Author.objects.filter(name="Jesus", email__contains="libresoft.es"
Author.objects.order_by("name", "-email")
Author.objects.filter(name="").order_by("name")
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 14
Introduction to URLconfs
Regular Expressions
Map URL patterns to views
from django.conf.urls.defaults import *
urlpatterns = patterns("",
(r"^mysite/", include("mysite.urls")),
(r"^admin/", include("django.contrib.admin.urls")),
)
logo
Santiago Dueñas sduenas@libresoft.es - 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@libresoft.es - October 7th, 2010 Web Development with Django 16
URLconf Example
urlpatterns = patterns("",
(r"^id/(?P<name>[A-Za-z\-]+)$", "mysite.views.id"),
(r"^id/(?P<name>[A-Za-z\-]+)/auth/(?P<type>[a-z]+)$",
"mysite.views.auths"),
(r"^/num/(?P<num>d{2})$", "mysite.views.id"),
(r"(?P<path>.*)$", "django.views.static.serve",
{"document_root": "/var/www/myproject/"},
)
logo
Santiago Dueñas sduenas@libresoft.es - 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@libresoft.es - October 7th, 2010 Web Development with Django 18
Templates
logo
Santiago Dueñas sduenas@libresoft.es - 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@libresoft.es - October 7th, 2010 Web Development with Django 20
Tags (ii)
include
{% include "index.html" %}
block
{% block title %}
<h1>Title</h1>
{% endblock %}
extends
{% extends "base.html" %}
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 21
More on Django
Administration Site
Forms
Template Engine Extensions
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 22
References
logo
Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 23