Download pdf - Useful Django 1.4

Transcript
Page 1: Useful Django 1.4

Django & Pyramid Con 2012@hirokiky

Useful Django1.4

12年9月18日火曜日

Page 2: Useful Django 1.4

Speaker: @hirokiky

Useful Django1.4

• About me (2m)

• About Django (19m)

• Useful things of Django > 1.0 (19m)

12年9月18日火曜日

Page 3: Useful Django 1.4

Speaker: @hirokiky

Useful Django1.4

• About me

• About Django• Useful things of Django > 1.0

12年9月18日火曜日

Page 4: Useful Django 1.4

Speaker: @hirokiky

About me

• @hirokiky (Hiroki KIYOHARA)

• Weekend Programmer

• Admin of djangoproject.jp

12年9月18日火曜日

Page 5: Useful Django 1.4

Speaker: @hirokiky

djangoproject.jp

http://djangoproject.jp/https://github.com/django-ja/djangoproject.jp/

12年9月18日火曜日

Page 6: Useful Django 1.4

Speaker: @hirokiky

Useful Django1.4

• About me

• About Django• Useful things of Django > 1.0

12年9月18日火曜日

Page 7: Useful Django 1.4

Speaker: @hirokiky

Django is a Open Source Web framework written by Python.

12年9月18日火曜日

Page 8: Useful Django 1.4

Speaker: @hirokiky

DjangoSites

• Disqus• Instagram• Mozilla

• bitbucket

12年9月18日火曜日

Page 9: Useful Django 1.4

Speaker: @hirokiky

WebServer

URL Dispatcher

Model

View

Template

DataBase

request

response

12年9月18日火曜日

Page 10: Useful Django 1.4

Speaker: @hirokiky

Philosophies• Loose coupling

• Less code

• Quick development

• Don’t repeat yourself (DRY)

• Explicit is better than implict

• Consistencyhttps://docs.djangoproject.com/en/1.4/misc/design-philosophies/

12年9月18日火曜日

Page 11: Useful Django 1.4

Speaker: @hirokiky

Killer features

•Automatically-generated admin site

•Form library

•Generic views

12年9月18日火曜日

Page 12: Useful Django 1.4

Speaker: @hirokiky

Admin site

•Automatically-generated

•Easy to customize

12年9月18日火曜日

Page 13: Useful Django 1.4

Speaker: @hirokiky

Demo: Admin (15m)

• startproject, create models, run admin site(10m)

• Introduction of django-blog-zinnia (5m)

12年9月18日火曜日

Page 14: Useful Django 1.4

Speaker: @hirokiky

Read Docsand some codes

If you want to learn more

12年9月18日火曜日

Page 15: Useful Django 1.4

Speaker: @hirokiky

Useful Django1.4

• About me

• About Django• Useful things of Django > 1.0

12年9月18日火曜日

Page 16: Useful Django 1.4

Speaker: @hirokiky

Django > 1.0

•project template• <del>Class Based View</del> <!-- Because of time constraints -->

12年9月18日火曜日

Page 17: Useful Django 1.4

Speaker: @hirokiky

project template

• Django>=1.4• template of project

https://docs.djangoproject.com/en/dev/releases/1.4/#custom-project-and-app-templates

12年9月18日火曜日

Page 18: Useful Django 1.4

Speaker: @hirokiky

Project template provide

• easy deploy to a specific environment

• archiving some best practices

12年9月18日火曜日

Page 19: Useful Django 1.4

Speaker: @hirokiky

Boring tasks after startproject

• Judging environments (DEBUG, True or False.

• A application should put in project/apps. (not default, but good know-how

• Writing dependences.

• Dealing informations we want to write in settings (SECRET_KEY, PASSWORD

12年9月18日火曜日

Page 20: Useful Django 1.4

Speaker: @hirokiky

Solving all of these by “templating”

12年9月18日火曜日

Page 21: Useful Django 1.4

Speaker: @hirokiky

django-admin.py startproject --template=/path/to/template myproject

--template=/path/to/template

also URL

How to useproject template

12年9月18日火曜日

Page 22: Useful Django 1.4

Speaker: @hirokiky

template├──  fabfile.py├──  gunicorn.py.ini├──  manage.py├──  Procfile├──  reqs│      ├──  common.txt│      ├──  dev.txt│      └──  prod.txt├──  requirements.txt├──  project_name│      ├──  apps│      │      └──  __init__.py│      ├──  __init__.py│      ├──  libs│      │      └──  __init__.py│      ├──  settings│      │      ├──  common.py│      │      ├──  dev.py│      │      ├──  __init__.py│      │      └──  prod.py│      ├──  templates│      │      ├──  404.html│      │      └──  500.html│      └──  urls.py└──  wsgi.py

template is like this(A directory)

12年9月18日火曜日

Page 23: Useful Django 1.4

Speaker: @hirokiky

template├──  fabfile.py├──  gunicorn.py.ini├──  manage.py├──  Procfile├──  reqs│      ├──  common.txt│      ├──  dev.txt│      └──  prod.txt├──  requirements.txt├──  project_name│      ├──  apps│      │      └──  __init__.py│      ├──  __init__.py│      ├──  libs│      │      └──  __init__.py│      ├──  settings│      │      ├──  common.py│      │      ├──  dev.py│      │      ├──  __init__.py│      │      └──  prod.py│      ├──  templates│      │      ├──  404.html│      │      └──  500.html│      └──  urls.py└──  wsgi.py

This name will be replaced withproject’s name

template is like this

12年9月18日火曜日

Page 24: Useful Django 1.4

Speaker: @hirokiky

if  __name__  ==  "__main__":        os.environ.setdefault("DJANGO_SETTINGS_MODULE",  "{{  project_name  }}.settings.dev")

       from  django.core.management  import  execute_from_command_line

       execute_from_command_line(sys.argv)

• project_name

• project_directory

• secret_key

• Also another files specified by option

Also .py files

12年9月18日火曜日

Page 25: Useful Django 1.4

Speaker: @hirokiky

In essenceYou can use own project template, instead of django/conf/project_template

12年9月18日火曜日

Page 26: Useful Django 1.4

Speaker: @hirokiky

Example: django-skel

• heroku + S3 platform• Filled with best practices that rdegges have

learned for four years.

https://github.com/rdegges/django-skel

12年9月18日火曜日

Page 27: Useful Django 1.4

Speaker: @hirokiky

Workflow

12年9月18日火曜日

Page 28: Useful Django 1.4

Speaker: @hirokiky

Before developing

• startproject --template ...

• git init• pip install -r reqs/dev.txt• sync, migrate, runserver

12年9月18日火曜日

Page 29: Useful Django 1.4

Speaker: @hirokiky

Hack some apps(not projects)

12年9月18日火曜日

Page 30: Useful Django 1.4

Speaker: @hirokiky

Before deploying

• fab  bootstrap

• heroku  config:add  ...

• heroku  scale  ...

• collectstatic  &&  compress

12年9月18日火曜日

Page 31: Useful Django 1.4

Speaker: @hirokiky

Good joooooo( ́∀`)oooooob!!!!

12年9月18日火曜日

Page 32: Useful Django 1.4

Speaker: @hirokiky

Let’s learn best practices from django-skel

•Judging environments

•apps directory

12年9月18日火曜日

Page 33: Useful Django 1.4

Speaker: @hirokiky

Note:django-skel’s

layout

django-­‐skel├──  fabfile.py├──  gunicorn.py.ini├──  manage.py├──  Procfile├──  reqs│      ├──  common.txt│      ├──  dev.txt│      └──  prod.txt├──  requirements.txt├──  project_name│      ├──  apps│      │      └──  __init__.py│      ├──  __init__.py│      ├──  libs│      │      └──  __init__.py│      ├──  settings│      │      ├──  common.py│      │      ├──  dev.py│      │      ├──  __init__.py│      │      └──  prod.py│      ├──  templates│      │      ├──  404.html│      │      └──  500.html│      └──  urls.py└──  wsgi.py

12年9月18日火曜日

Page 34: Useful Django 1.4

Speaker: @hirokiky

Judging environments

• Judging environments automaticaly.

• Don’t do hardcoding (settings.DEBUG = True

12年9月18日火曜日

Page 35: Useful Django 1.4

Speaker: @hirokiky

On django-skel

• Only  development  setting  will  put  in  dev.py

• settings  for  production  =>  prod.py

• Common  settings  =>  common.py

• heroku  config:add  DJANGO_SETTINGS_MODULE={{  project_name  }}.settings.prod

├──  settings│      │      ├──  __init__.py│      │      ├──  common.py│      │      ├──  dev.py│      │      └──  prod.py

12年9月18日火曜日

Page 36: Useful Django 1.4

Speaker: @hirokiky

apps directory

• Directory for applications

• For avoiding import collision

12年9月18日火曜日

Page 37: Useful Django 1.4

Speaker: @hirokiky

Here

django-­‐skel├──  fabfile.py├──  gunicorn.py.ini├──  manage.py├──  Procfile├──  reqs│      ├──  common.txt│      ├──  dev.txt│      └──  prod.txt├──  requirements.txt├──  project_name│      ├──  apps│      │      └──  __init__.py│      ├──  __init__.py│      ├──  libs│      │      └──  __init__.py│      ├──  settings│      │      ├──  common.py│      │      ├──  dev.py│      │      ├──  __init__.py│      │      └──  prod.py│      ├──  templates│      │      ├──  404.html│      │      └──  500.html│      └──  urls.py└──  wsgi.py

12年9月18日火曜日

Page 38: Useful Django 1.4

Speaker: @hirokiky

If no apps/.  #  here,  path|-­‐-­‐  aggregator|      |-­‐-­‐  __init__.py|      |-­‐-­‐  blog.py|      |-­‐-­‐  models.py|      |-­‐-­‐  tests.py|      `-­‐-­‐  views.py|-­‐-­‐  blog|      |-­‐-­‐  __init__.py|      |-­‐-­‐  models.py|      |-­‐-­‐  tests.py|      `-­‐-­‐  views.py|-­‐-­‐  manage.py`-­‐-­‐  myprj        |-­‐-­‐  __init__.py        |-­‐-­‐  settings.py        |-­‐-­‐  urls.py        `-­‐-­‐  wsgi.py

• Ex: In aggregator.views• from blog.models import Entry

• ImportError

12年9月18日火曜日

Page 39: Useful Django 1.4

Speaker: @hirokiky

Then apps/

• from apps.blog.model import Entry

.  #  here,  path|-­‐-­‐  apps|      |-­‐-­‐  __init__.py|      |-­‐-­‐  aggregator|      |      |-­‐-­‐  __init__.py|      |      |-­‐-­‐  blog.py|      |      |-­‐-­‐  models.py|      |      |-­‐-­‐  tests.py|      |      `-­‐-­‐  views.py|      `-­‐-­‐  blog|              |-­‐-­‐  __init__.py|              |-­‐-­‐  models.py|              |-­‐-­‐  tests.py|              `-­‐-­‐  views.py|-­‐-­‐  manage.py`-­‐-­‐  myprj        |-­‐-­‐  __init__.py        |-­‐-­‐  settings.py        |-­‐-­‐  urls.py        `-­‐-­‐  wsgi.py

12年9月18日火曜日

Page 40: Useful Django 1.4

Speaker: @hirokiky

Project template provide

• easy deploy to a specific environment

• archiving some best practices

12年9月18日火曜日

Page 41: Useful Django 1.4

Speaker: @hirokiky

Luke of Django

• Deploy project avoiding HELL• Learn best practices through reading good project templates

12年9月18日火曜日

Page 42: Useful Django 1.4

Speaker: @hirokiky

Yoda of Django

• Without remembering best practices and adapting to new project to these

• Create Django’s default standards in the form of project template

12年9月18日火曜日

Page 43: Useful Django 1.4

Speaker: @hirokiky

Killer Feature of Django1.4is not only {% elif %}

12年9月18日火曜日

Page 44: Useful Django 1.4

Speaker: @hirokiky

Announce

• I held DjangoSprint• 2012 September 17

• It don’t force you what to do.• http://2012.pycon.jp/en/program/sprints.html

12年9月18日火曜日

Page 45: Useful Django 1.4

Speaker: @hirokiky

Thank you

12年9月18日火曜日

Page 46: Useful Django 1.4

Speaker: @hirokiky

Any questions?

12年9月18日火曜日

Page 47: Useful Django 1.4

Speaker: @hirokiky

References

• djangoproject.jp• djangoproject.com• django-blog-zinnia• django-skel

12年9月18日火曜日

Page 48: Useful Django 1.4

Speaker: @hirokiky

• We are translating Django1.4 documents to Japanese

https://github.com/django-docs-ja/django-docs-ja

django-docs-ja

12年9月18日火曜日