Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
django入門
Search
naoki-urabe
April 16, 2022
Programming
120
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
django入門
naoki-urabe
April 16, 2022
More Decks by naoki-urabe
See All by naoki-urabe
無駄課金回避__lambdaでEC2定期シャットダウン.pdf
naokiurabe
0
270
DevOps勉強会資料
naokiurabe
0
160
django入門2
naokiurabe
0
88
Other Decks in Programming
See All in Programming
JIS keyboard support in Omarchy
lef237
0
110
Workers Cache を知る
syumai
0
360
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
750
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
190
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
500
ChromeでローカルLLM
ryu1013
0
140
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
270
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
220
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
0
920
TechTown - Creating Composable Callables in Contemporary C++
rollbear
0
130
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
220
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
The Invisible Side of Design
smashingmag
301
52k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
Side Projects
sachag
456
43k
A Tale of Four Properties
chriscoyier
163
24k
Making Projects Easy
brettharned
120
6.8k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
260
A designer walks into a library…
pauljervisheath
211
25k
30 Presentation Tips
portentint
PRO
1
430
Code Reviewing Like a Champion
maltzj
529
40k
Transcript
Django入門
Djangoとは? • pythonベースのwebフレームワーク • 中規模〜大規模のアプリケーション開発が得意 • Djangoのメリット − セキュリティに強い −
データベース構築が簡単
MTVモデル • DjangoはMTVモデルを採用している − MCVのVをTemplateとViewに明確に分けている • Model データを保持する • Template
表示する画面 • View リクエストオブジェクトを受取、レスポンスオ ブジェクトを生成し返す
Model • Django ORM(Object Relational Mapper)を提供 している • データベースのテーブル・カラムとモデルのクラスと フィールドを対応付け、レコード1件毎にオブジェク
トとして扱えるようにしている クラス フィールド データベース テーブル カラム
View • リクエストオブジェクトを受取、レスポンスオブジェク トを生成し返す • ビジネスロジックを実行し、テンプレートに表示する 変数を用意する func(1) func(2) Context
= { Name: test Age: 10 } Template
Template • DTL(Django Template Language)を提供している • Htmlに変数表示やロジックを埋め込むことができ る {{ variable
}} 変数表示 {% if is_active %} 条件分岐 Hello {% endif %} {% for item in items %} 反復 {{ item.value }} {% endfor %}
MVCのCは? • URLディスパッチャがControllerの役目 • リクエストとして送られたURLを識別し適切なview 関数へ渡す URLConf URLディスパッチャ / /manage_employ
ee /login リクエスト
Djangoの処理の流れ Url:http:1337/test URLディス パッチャ View Model データベース レスポンス 適切なView関数に リクエストを渡す
Url:http:1337/test Url:http:1337/test データベースから情報を取得 レスポンスを生成し返 す データベース操 作
実際に作ってみよう 従業員情報を出力するアプリを作ってみる https://github.com/naoki-urabe/django-app django-appというフォルダを作り、そこで作成していく
Dockerで環境構築
Docker-composeを利用する
環境変数分離 • 秘密鍵情報等は.envファイルに分けたほうが良い
環境変数分離 • 秘密鍵情報等は.envファイルに分けたほうが良い
Djangoプロジェクト作成 • プロジェクトの作成コマンドによってプロジェクトを 作成
docker-composeでコンテナ立ち上げ • Localhost:8000を開く • ロケットの画面が出れば成功
URL設定 • URLConfに該当 • プロジェクトのURL.pyにURLパターンを設定する • 上の例では /にアクセスした際、 manage_employee.urlを見に行く
アプリケーションを作成 • djangoではアプリケーションという小さいサービス を連携させ全体のアプリケーションを構築している • 上記コマンドを打つことでアプリケーションのフォル ダができる
Urls.py • URLConfに該当 • アプリケーションごとにUrls.pyを作成する • プロジェクトのUrl.pyを複雑化させないため
Model定義 • テーブル・カラムの定義を行う • データベースに保存される形式を定義している
migration • Model定義の新規作成・変更の場合はデータベー スに対し変更(migration)を行わなければならない • migrationの実行はmakemakegrationsコマンド migrateコマンド
Viewの設定 • リクエストを受取り、ビジネスロジックを実行し、レス ポンス生成して返す
Templateの設定 • Viewから渡された変数を表示する
表示画面 • 従業員名・年齢が表示されれば成功
ポイント • DjangoはMTVモデルを採用している − Model Django ORMマッパー − T Django
Template Engine − V リクエストを受取、レスポンスを返す − MCVのCに当たるのはURLディスパッチャ • 開発時 − 秘密鍵等は.envファイルに分割する − モデルを追加変更した際はmigrationする − URL.pyはアプリケーションごとに追加する