Intro to Django
Last updated
Was this helpful?
Last updated
Was this helpful?
: Python web framework
High-level Python web framework that enables rapid development of secure and maintainable websites
Free & open source
web page ๊ฐ๋ฐํ๋ ๊ณผ์ ์์ ๊ฒช๋ ์ด๋ ค์์ ์ค์ด๋ ๊ฒ์ด ๋ชฉ์
์์ฉ ํ๋ก๊ทธ๋จ์ ํ์ค ๊ตฌ์กฐ ๋ฅผ ๊ตฌํํ๋ class์ library์ ๋ชฉ๋ก
Portable
Python based which means it runs on may platform
Complete
Django follows the "Batteries included" philosophy
Everything you need is part of the one "product"
Works seamlessly together
Follows consistent design principles
Has extensive and .
Versatile
Can be used to build almost any type of website
Can work with any client-side framework
Can deliver content in almost any format (including HTML, RSS feeds, JSON, XML, etc)
Secure
Provides a secure way to manage user accounts & passwords
By avoiding common mistakes like putting session information in cookies where it is vulnerable
Instead cookies just contain a key
And the actual data is stored in the db
By avoiding directly store passwords rather than password hash
Scalable
Uses a component-based "shared-nothing" architecture
each part of the architecture is independent of the others
can be replaced or changed if needed
Can scale for increase traffic by adding hardware at any level
caching servers
database servers
application servers
Maintainable
Follows DRY (Don't Repeat Yourself) principle
No unnecessary duplication
Promotes the grouping of related functionality into reusable
๋ง์ ๊ธฐ์ ์์ ํ์ฉ์ค
Spotify
Dropbox
Delivery Hero
etc.
์์ฒญ (request)
์๋ต (response)
django๋ MVC (Model View Controller) pattern ์ ๋ฐ๋ฅด๊ณ ์๋ค.
MVC: Software design pattern ์ค ํ๋
MVC Pattern
django
Model
Model
View
Template
Controller
View
MTV
M: ๋ฐ์ดํฐ ๊ด๋ฆฌ
T: ์ธํฐํ์ด์ค (ํ๋ฉด)
V: ์ค๊ฐ ๊ด๋ฆฌ (์ํธ ๋์)
Cloud ๊ธฐ๋ฐ IDE (ํตํฉ ๊ฐ๋ฐ ํ๊ฒฝ)
OS: Ubuntu 18.04.4 LTS
python 3.7.6
๊ฐ๋ฐ local ํ๊ฒฝ์ ์ํฅ์ด ์๋ค๋ ์ฅ์ ์ด ์์
2.1.15 ๋ก ๋ค์ด๋ฐ๊ธฐ
settings.py 28 ๋ฒ์งธ ์ค ์์ ํ๊ธฐ
์๋ฒ ์คํ ์ ๋ฐ๋์ ๋ช ๋ น์ด๊ฐ ์คํ๋๋ directory๋ฅผ ํ์ธ ํ ๊ฒ
์คํ๋ ์๋ฒ๋ ์ฐ์ธก ์์ญ์ url์ clickํ๋ค
์๋ฒ ์ข
๋ฃ๋ terminal์์ ctrl + c
์๋ฒ ์คํ ๋ ํ๋ฉด
project
๋ django์ atomic ํ ๋จ์!
INSTALLED_APPS
: ์ฃผ๋ฏผ๋ฑ๋ก
์ฃผ๋ฏผ๋ฑ๋ก์ ์ํด pages
์ถ๊ฐํจ
80๋ฒ๊ณผ 8080์ผ๋ก ์ฐ๊ฒฐ๋์ด ์๋ ๊ฒ ํ์ธ ๊ฐ๋ฅ
django๋ DEBUG=True
๊ฐ default
๋ฌธ์ง๊ธฐ
path('์์ฒญ URL/', handling ํ view(controller) ํจ์)
url ๋์ /
๊ฐ ๋ถ๋๊ฒ django์ ํน์ง!
hello() ๊ฐ ๋ถ๋ฆด ๋๋ง๋ค ํจ์๋ฅผ ๋ถ๋ฅธ ๊ฐ์ฒด๊ฐ ์ฒซ๋ฒ์งธ ์ธ์๋ก ๋ค์ด๊ฐ
view์์ ํจ์๋ฅผ ์ ์ํ๋ ๊ฒฝ์ฐ ํญ์ ์ฒซ๋ฒ์งธ ์ธ์๋ฅผ request
๋ก ์ ์ํ๋ค!
์ฒซ ๋ฒ์งธ ์ธ์ - request
๋ ๋ฒ์งธ ์ธ์ - ํ์ผ ๋ช
์ธ ๋ฒ์งธ ์ธ์ - context
context๋ ํญ์ dictionary ๋ก ํด์ผํจ!
์ด๋ฆ์ด ๊ผญ templates
์ฌ์ผ ํ๋ค
django๋ ์ฌ๋ฌ๊ฐ์ ์ฑ์ ๊ฐ์ง ํ๋์ project๋ก ๊ตฌ์ฑ๋๋ค
ex) ์ปค๋ฎค๋ํฐ๋ฅผ ๋ง๋ ๋ค
ํ์๊ณผ ๊ด๋ จ๋ app - accounts
๊ฒ์๊ธ๊ณผ ๊ด๋ จ๋ app - posts
app์ ์์ฑํ๊ณ ๋ฐ๋์ settings.py
์ INSTALLED_APPS
์ ๋ฑ๋กํ๋ค
url.py
path ์ url์ ํญ์ /
๋ก ๋ซ์์ค๋ค
views.py
ํจ์๋ฅผ ์ ์ํ ๋, ํญ์ ์ฒซ๋ฒ ์งธ ์ธ์๋ request
๋ก ์์ฑํด๋๋ค
why? ๋ด๋ถ์ ์ผ๋ก ์์ฒญ์ ์ฒ๋ฆฌ ํ ๋, ํจ์ ํธ์ถ ์ ์์ฒญ ์ ๋ณด๊ฐ ๋ด๊ธด ๊ฐ์ฒด๋ฅผ ๋ณด๋ด์ค์ผ ํด์
render
ํจ์๋ฅผ ํตํด์ ๋ฐํํ๋ค
์ฒซ๋ฒ์งธ ์ธ์: reqeust
๋ ๋ฒ์งธ ์ธ์: template file (html
)
์ธ๋ฒ์งธ ์ธ์
dictionary type
template์์ ํ์ฉ ํ๋ ค๊ณ ํ๋ ๊ฐ๋ค์ ์ ๋ฌ
templates
ํ์ผ ์์ฑ๋ฐํํ html
file์ ํญ์ templates
folder ์์ ์์ฑํ๋ค
context dictionary์ key ๊ฐ์ ์ ์ผ๋ฉด ์ถ๋ ฅ๋๋ค
: CS50 IDE is a cloud-based Integrated Development Environment powered by that features a cloud-based Ubuntu environment, a browser-based editor that supports syntax highlighting and word completion, a GUI-based GDB debugging, themes, customizable layouts, keyboard shortcuts, and many more features. Since itโs cloud-based, you can continue working on your problem sets even if you use a different computer!