46
Getting started with Django Arun S.A.G Sastra University, Trichy February, 2012 Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 1 / 26

Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

  • Upload
    others

  • View
    15

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Getting started with Django

Arun S.A.G

Sastra University, Trichy

February, 2012

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 1 / 26

Page 2: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Outline

1 Django Overview

2 Installing django

3 Creating a new project

4 Creating a new application

5 Working with models

6 Working with templates

7 Views and URLConfs

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 2 / 26

Page 3: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

About Django

Web Framework

MVC (MVT)

ORM

Admin interface

Template system

Cache system

Internationalization

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 3 / 26

Page 4: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

About Django

Web Framework

MVC (MVT)

ORM

Admin interface

Template system

Cache system

Internationalization

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 3 / 26

Page 5: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

About Django

Web Framework

MVC (MVT)

ORM

Admin interface

Template system

Cache system

Internationalization

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 3 / 26

Page 6: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

About Django

Web Framework

MVC (MVT)

ORM

Admin interface

Template system

Cache system

Internationalization

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 3 / 26

Page 7: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

About Django

Web Framework

MVC (MVT)

ORM

Admin interface

Template system

Cache system

Internationalization

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 3 / 26

Page 8: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

About Django

Web Framework

MVC (MVT)

ORM

Admin interface

Template system

Cache system

Internationalization

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 3 / 26

Page 9: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

About Django

Web Framework

MVC (MVT)

ORM

Admin interface

Template system

Cache system

Internationalization

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 3 / 26

Page 10: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Outline

1 Django Overview

2 Installing django

3 Creating a new project

4 Creating a new application

5 Working with models

6 Working with templates

7 Views and URLConfs

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 4 / 26

Page 11: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Getting started

Getting Django

Install

Fedora yum install django

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 5 / 26

Page 12: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Outline

1 Django Overview

2 Installing django

3 Creating a new project

4 Creating a new application

5 Working with models

6 Working with templates

7 Views and URLConfs

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 6 / 26

Page 13: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Getting started

Starting a project

New Project

django-admin startproject blog

blog/

init .py

manage.py

settings.py

urls.py

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 7 / 26

Page 14: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Getting started

Starting a project

New Project

django-admin startproject blog

blog/

init .py

manage.py

settings.py

urls.py

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 7 / 26

Page 15: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Getting started

Starting a project

New Project

django-admin startproject blog

blog/

init .py

manage.py

settings.py

urls.py

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 7 / 26

Page 16: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Getting started

Starting a project

New Project

django-admin startproject blog

blog/

init .py

manage.py

settings.py

urls.py

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 7 / 26

Page 17: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Getting started

Starting a project

New Project

django-admin startproject blog

blog/

init .py

manage.py

settings.py

urls.py

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 7 / 26

Page 18: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Outline

1 Django Overview

2 Installing django

3 Creating a new project

4 Creating a new application

5 Working with models

6 Working with templates

7 Views and URLConfs

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 8 / 26

Page 19: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

First Django application

Creating an application

New app

django-admin startapp posts

posts/

init .py

models.py

views.py

tests.py

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 9 / 26

Page 20: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

First Django application

Creating an application

New app

django-admin startapp posts

posts/

init .py

models.py

views.py

tests.py

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 9 / 26

Page 21: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

First Django application

Creating an application

New app

django-admin startapp posts

posts/

init .py

models.py

views.py

tests.py

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 9 / 26

Page 22: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

First Django application

Creating an application

New app

django-admin startapp posts

posts/

init .py

models.py

views.py

tests.py

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 9 / 26

Page 23: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

First Django application

Creating an application

New app

django-admin startapp posts

posts/

init .py

models.py

views.py

tests.py

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 9 / 26

Page 24: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Outline

1 Django Overview

2 Installing django

3 Creating a new project

4 Creating a new application

5 Working with models

6 Working with templates

7 Views and URLConfs

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 10 / 26

Page 25: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

First model

posts/models.py

from django.db import models

class Post(models.Model):

"""

Model that holds blog posts

"""

title = models.CharField(max_length=200)

body = models.TextField()

posted_date = models.DateTimeField(

default=datetime.now,blank=True)

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 11 / 26

Page 26: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Database configuration

Configure the database

settings.py

DATABASES = {

’default’: {

’ENGINE’: ’django.db.backends.sqlite3’,

’NAME’: ’blog.sqlite3’,

’USER’: ’’,

’PASSWORD’: ’’,

’HOST’: ’’,

’PORT’: ’’,

}

}

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 12 / 26

Page 27: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Activating the model

Add the app to INSTALLED APPS section

settings.py

INSTALLED_APPS = (

’django.contrib.auth’,

’django.contrib.contenttypes’,

’django.contrib.sessions’,

’django.contrib.sites’,

’django.contrib.messages’,

# User defined app

’blog.posts’,

)

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 13 / 26

Page 28: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Previewing the SQL

Command

python manage.py sqlall posts

Generated SQLBEGIN;

CREATE TABLE "posts_post" (

"id" integer NOT NULL PRIMARY KEY,

"title" varchar(200) NOT NULL,

"body" varchar(1000) NOT NULL,

"posted_date" datetime NOT NULL,

)

;

COMMIT;

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 14 / 26

Page 29: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Previewing the SQL

Command

python manage.py sqlall posts

Generated SQLBEGIN;

CREATE TABLE "posts_post" (

"id" integer NOT NULL PRIMARY KEY,

"title" varchar(200) NOT NULL,

"body" varchar(1000) NOT NULL,

"posted_date" datetime NOT NULL,

)

;

COMMIT;

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 14 / 26

Page 30: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Populating the tables

syncdb

python manage.py syncdb

Creating table posts_post

No fixtures found.

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 15 / 26

Page 31: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Populating the tables

syncdb

python manage.py syncdbCreating table posts_post

No fixtures found.

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 15 / 26

Page 32: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Outline

1 Django Overview

2 Installing django

3 Creating a new project

4 Creating a new application

5 Working with models

6 Working with templates

7 Views and URLConfs

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 16 / 26

Page 33: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Base Template

templates/base.html

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

{% block title %}

{% endblock %}

</head>

<body>

{% block content %}

{% endblock %}

</body>

</html>Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 17 / 26

Page 34: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Extending base template

templates/home.html

{% extends ’base.html’ %}

{% block title %}

<title> Blog - A simple personal blog </title>

{% endblock %}

{% block content %}

{% for post in posts %}

<h2><a href=post/{{ post.id }}>{{ post.title }}</a></h2>

<p>{{ post.body }} </p>

<hr/>

{% endfor %}

{% endblock %}

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 18 / 26

Page 35: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Adding template directory to settings.py

settings.pytry:

ROOT_DIR

except NameError:

ROOT_DIR = os.path.dirname(__file__)

settings.py

TEMPLATE_DIRS = (

os.path.join(ROOT_DIR,’templates’),

)

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 19 / 26

Page 36: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Outline

1 Django Overview

2 Installing django

3 Creating a new project

4 Creating a new application

5 Working with models

6 Working with templates

7 Views and URLConfs

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 20 / 26

Page 37: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Writing your first view

posts/views.py

def home(request):

if request.method == ‘‘GET‘‘:

posts = Post.objects.all()

return direct_to_template(request,’home.html’,

{’posts’:posts})

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 21 / 26

Page 38: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Mapping URL to views

urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns(’blog.posts’,

(r’^$’,’views.home’),)

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 22 / 26

Page 39: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Testing the website

manage.py

python manage.py runserver

0 errors found

Django version 1.2.5, using settings ’blog.settings’

Development server is running at http://127.0.0.1:8000/

Quit the server with CONTROL-C

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 23 / 26

Page 40: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Testing the website

manage.py

python manage.py runserver0 errors found

Django version 1.2.5, using settings ’blog.settings’

Development server is running at http://127.0.0.1:8000/

Quit the server with CONTROL-C

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 23 / 26

Page 41: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Queries

Questions?

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 24 / 26

Page 42: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

References

The offcial website http://djangoproject.com/

Django book http://djangobook.com/

Created using beamer http://latex-beamer.sourceforge.net/

Source: https://gitorious.org/̃ sagarun

Licensed under GNU Free documentation license

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 25 / 26

Page 43: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Contact

Arun S.A.G

Email: [email protected]

IRC: zer0c00l

Website: http://zer0c00l.in/

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 26 / 26

Page 44: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Contact

Arun S.A.G

Email: [email protected]

IRC: zer0c00l

Website: http://zer0c00l.in/

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 26 / 26

Page 45: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Contact

Arun S.A.G

Email: [email protected]

IRC: zer0c00l

Website: http://zer0c00l.in/

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 26 / 26

Page 46: Getting started with Django · django-admin startapp posts posts/ init .py models.py views.py tests.py Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February,

Contact

Arun S.A.G

Email: [email protected]

IRC: zer0c00l

Website: http://zer0c00l.in/

Arun S.A.G (Sastra University, Trichy) Web Framework for perfectionists February, 2012 26 / 26