使えるDjango1.4
description
Transcript of 使えるDjango1.4
Django & Pyramid Con 2012@hirokiky
使えるDjango1.4
12年9月14日金曜日
Speaker: @hirokiky
使えるDjango1.4
• おまえだれよ (2m)
• Django について (24m)
• Django > 1.0の使えること (19m)
12年9月14日金曜日
Speaker: @hirokiky
使えるDjango1.4
• おまえだれよ
• Django について
• Django > 1.0の使えること
12年9月14日金曜日
Speaker: @hirokiky
おまえだれよ
•@hirokiky (清原 弘貴)
• Weekend Programmer
• Admin of djangoproject.jp
12年9月14日金曜日
Speaker: @hirokiky
djangoproject.jp
http://djangoproject.jp/https://github.com/django-ja/djangoproject.jp/
12年9月14日金曜日
Speaker: @hirokiky
使えるDjango1.4
• おまえだれよ
• Django について
• Django > 1.0の使えること
12年9月14日金曜日
Speaker: @hirokiky
DjangoはPythonで書かれたオープンソースのWebフレームワーク
12年9月14日金曜日
Speaker: @hirokiky
Django製サイト
•Disqus
• Mozilla
• bitbucket
12年9月14日金曜日
Speaker: @hirokiky
WebServer
URL Dispatcher
Model
View
Template
DataBase
request
response
12年9月14日金曜日
Speaker: @hirokiky
設計思想• ルースカップリング
• コード量の低減
• 迅速な開発
• DRY(Don’t repeat yourself)則
• 暗示的より明示的に
• 一貫性http://djangoproject.jp/doc/ja/1.0/misc/design-philosophies.html
12年9月14日金曜日
Speaker: @hirokiky
Djangoの良いところ
•オブジェクト管理インタフェース•フォーム処理ライブラリ•汎用ビュー
12年9月14日金曜日
Speaker: @hirokiky
オブジェクト管理インタフェース(Admin)
• 管理インタフェースが動的に生成される
• カスタマイズ性が高い
12年9月14日金曜日
Speaker: @hirokiky
Demo: Admin (15m)
• プロジェクト作成 => 管理画面 (10m)
• django-blog-zinniaの紹介 (5m)
12年9月14日金曜日
Speaker: @hirokiky
Read Docsand some codes
12年9月14日金曜日
Speaker: @hirokiky
使えるDjango1.4
• おまえだれよ
• Django について
• Django > 1.0の使えること
12年9月14日金曜日
Speaker: @hirokiky
Django > 1.0
•project template• <del>Class Based View</del>
12年9月14日金曜日
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月14日金曜日
Speaker: @hirokiky
project templateで?
•特定環境へのデプロイが楽
• ノウハウをまとめておける
12年9月14日金曜日
Speaker: @hirokiky
色々めんどくさい
• 開発/運用環境の切り分け(DEBUG
• アプリケーションはproject/appsに置きたい
• 依存パッケージの記述
• 設定ファイルに書きたくない情報の扱い (SECRET_KEY, PASSWORD
12年9月14日金曜日
Speaker: @hirokiky
これを『テンプレ』化
12年9月14日金曜日
Speaker: @hirokiky
django-admin.py startproject --template=/path/to/template myproject
--template=/path/to/template
※URLも可
使い方
12年9月14日金曜日
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はこんな感じ(ディレクトリ)
12年9月14日金曜日
Speaker: @hirokiky
templateはこんな感じ(ディレクトリ)
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
プロジェクト名に置き換わる
12年9月14日金曜日
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
• あと自由にオプションで
.pyファイルも
12年9月14日金曜日
Speaker: @hirokiky
要するに
django/conf/project_templateの代わりにオレオレproject templateが使える
12年9月14日金曜日
Speaker: @hirokiky
例: django-skel
• heroku + S3 環境
• Django歴4年以上の開発者のノウハウがいっぱい
https://github.com/rdegges/django-skel
12年9月14日金曜日
Speaker: @hirokiky
Workflow
12年9月14日金曜日
Speaker: @hirokiky
開発まえ
• startproject
• git init
• pip install -r reqs/dev.txt
• sync, migrate, runserver
12年9月14日金曜日
Speaker: @hirokiky
Hack some apps(not projects)
12年9月14日金曜日
Speaker: @hirokiky
運用まで
• fab bootstrap
• heroku config:add
• heroku scale
• collectstatic && compress
12年9月14日金曜日
Speaker: @hirokiky
オツカレチャ━━━━( ́∀`)━━━━ン!!!!
12年9月14日金曜日
Speaker: @hirokiky
django-skelからノウハウを学ぶ
•開発/運用環境の切り分け
•appsディレクトリ
12年9月14日金曜日
Speaker: @hirokiky
django-skelのレイアウト
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月14日金曜日
Speaker: @hirokiky
環境の切り分け
•開発、運用環境をうまく切り替えたい
• settings.DEBUGをハードコードは最悪
12年9月14日金曜日
Speaker: @hirokiky
django-skelでは?
• 開発のみの設定はdev、運用はprod、共通がcommon
• heroku config:add DJANGO_SETTINGS_MODULE={{ project_name }}.settings.prod
├── settings│ │ ├── __init__.py│ │ ├── common.py│ │ ├── dev.py│ │ └── prod.py
12年9月14日金曜日
Speaker: @hirokiky
apps
• アプリケーションをいれる
• import時の名前の衝突を防ぐ
12年9月14日金曜日
Speaker: @hirokiky
django-skelのここ
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月14日金曜日
Speaker: @hirokiky
apps無しの例. # ここに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
• aggregator.viewsで
• from blog.models import Entry
• ImportError
12年9月14日金曜日
Speaker: @hirokiky
そこでapps
• from apps.blog.model import Entry
. # ここに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月14日金曜日
Speaker: @hirokiky
project templateで?
•特定環境へのデプロイが楽
• ノウハウをまとめておける
12年9月14日金曜日
Speaker: @hirokiky
Django1.4の良さは {% elif %} だけじゃない
12年9月14日金曜日
Speaker: @hirokiky
Announce
• DjangoSprintやります
• PyConJP 3日目
• 自由に
• http://2012.pycon.jp/program/sprints.html
12年9月14日金曜日
Speaker: @hirokiky
以上
12年9月14日金曜日
Speaker: @hirokiky
Any questions?
12年9月14日金曜日
Speaker: @hirokiky
参考資料
• djangoproject.jp
• djangoproject.com
• django-blog-zinnia
• django-skel
12年9月14日金曜日
Speaker: @hirokiky
なんで Django > 1.0
• 日本語ドキュメントが長らく1.0
• Django1.4ドキュメント翻訳作業中
• django-docs-ja
https://github.com/django-docs-ja/django-docs-ja
12年9月14日金曜日