0% found this document useful (0 votes)
15 views

Django

Uploaded by

gokbilimci6510
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Django

Uploaded by

gokbilimci6510
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Web Development with Django

Santiago Dueñas
sduenas@libresoft.es

October 7th, 2010

Santiago Dueñas sduenas@libresoft.es - 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)
http://www.djangoproject.com

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)

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@libresoft.es - 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
django-admin.py startproject <PROJECT>
django-admin.py startapp <APP>
manage.py runserver [DOMAIN]

logo

Santiago Dueñas sduenas@libresoft.es - 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@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

All classes inherit from models.Model


from django.db import models

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

ForeignKey(othermodel) -> 1-N


a.fk = b
OneToOneField(othermodel) -> 1-1 (FK with unique=True)
a.fk = b
ManyToManyField(othermodel) -> N-N
a.fk.add(b)

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

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@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

Django Book http://www.djangobook.com/


Django Documentation
http://docs.djangoproject.com/en/dev/

logo

Santiago Dueñas sduenas@libresoft.es - October 7th, 2010 Web Development with Django 23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy