Django & Pyramid Con 2012@hirokiky
Useful Django1.4
12年9月18日火曜日
Speaker: @hirokiky
Useful Django1.4
• About me (2m)
• About Django (19m)
• Useful things of Django > 1.0 (19m)
12年9月18日火曜日
Speaker: @hirokiky
Useful Django1.4
• About me
• About Django• Useful things of Django > 1.0
12年9月18日火曜日
Speaker: @hirokiky
About me
• @hirokiky (Hiroki KIYOHARA)
• Weekend Programmer
• Admin of djangoproject.jp
12年9月18日火曜日
Speaker: @hirokiky
djangoproject.jp
http://djangoproject.jp/https://github.com/django-ja/djangoproject.jp/
12年9月18日火曜日
Speaker: @hirokiky
Useful Django1.4
• About me
• About Django• Useful things of Django > 1.0
12年9月18日火曜日
Speaker: @hirokiky
Django is a Open Source Web framework written by Python.
12年9月18日火曜日
Speaker: @hirokiky
DjangoSites
• Disqus• Instagram• Mozilla
• bitbucket
12年9月18日火曜日
Speaker: @hirokiky
WebServer
URL Dispatcher
Model
View
Template
DataBase
request
response
12年9月18日火曜日
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日火曜日
Speaker: @hirokiky
Killer features
•Automatically-generated admin site
•Form library
•Generic views
12年9月18日火曜日
Speaker: @hirokiky
Admin site
•Automatically-generated
•Easy to customize
12年9月18日火曜日
Speaker: @hirokiky
Demo: Admin (15m)
• startproject, create models, run admin site(10m)
• Introduction of django-blog-zinnia (5m)
12年9月18日火曜日
Speaker: @hirokiky
Read Docsand some codes
If you want to learn more
12年9月18日火曜日
Speaker: @hirokiky
Useful Django1.4
• About me
• About Django• Useful things of Django > 1.0
12年9月18日火曜日
Speaker: @hirokiky
Django > 1.0
•project template• <del>Class Based View</del> <!-- Because of time constraints -->
12年9月18日火曜日
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日火曜日
Speaker: @hirokiky
Project template provide
• easy deploy to a specific environment
• archiving some best practices
12年9月18日火曜日
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日火曜日
Speaker: @hirokiky
Solving all of these by “templating”
12年9月18日火曜日
Speaker: @hirokiky
django-admin.py startproject --template=/path/to/template myproject
--template=/path/to/template
also URL
How to useproject template
12年9月18日火曜日
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日火曜日
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日火曜日
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日火曜日
Speaker: @hirokiky
In essenceYou can use own project template, instead of django/conf/project_template
12年9月18日火曜日
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日火曜日
Speaker: @hirokiky
Workflow
12年9月18日火曜日
Speaker: @hirokiky
Before developing
• startproject --template ...
• git init• pip install -r reqs/dev.txt• sync, migrate, runserver
12年9月18日火曜日
Speaker: @hirokiky
Hack some apps(not projects)
12年9月18日火曜日
Speaker: @hirokiky
Before deploying
• fab bootstrap
• heroku config:add ...
• heroku scale ...
• collectstatic && compress
12年9月18日火曜日
Speaker: @hirokiky
Good joooooo( ́∀`)oooooob!!!!
12年9月18日火曜日
Speaker: @hirokiky
Let’s learn best practices from django-skel
•Judging environments
•apps directory
12年9月18日火曜日
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日火曜日
Speaker: @hirokiky
Judging environments
• Judging environments automaticaly.
• Don’t do hardcoding (settings.DEBUG = True
12年9月18日火曜日
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日火曜日
Speaker: @hirokiky
apps directory
• Directory for applications
• For avoiding import collision
12年9月18日火曜日
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日火曜日
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日火曜日
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日火曜日
Speaker: @hirokiky
Project template provide
• easy deploy to a specific environment
• archiving some best practices
12年9月18日火曜日
Speaker: @hirokiky
Luke of Django
• Deploy project avoiding HELL• Learn best practices through reading good project templates
12年9月18日火曜日
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日火曜日
Speaker: @hirokiky
Killer Feature of Django1.4is not only {% elif %}
12年9月18日火曜日
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日火曜日
Speaker: @hirokiky
Thank you
12年9月18日火曜日
Speaker: @hirokiky
Any questions?
12年9月18日火曜日
Speaker: @hirokiky
References
• djangoproject.jp• djangoproject.com• django-blog-zinnia• django-skel
12年9月18日火曜日
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日火曜日