Django Basics
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Introduction
What is django?
: Python web framework
High-level Python web framework that enables rapid development of secure and maintainable websites
Free & open source
web framework
web page ๊ฐ๋ฐํ๋ ๊ณผ์ ์์ ๊ฒช๋ ์ด๋ ค์์ ์ค์ด๋ ๊ฒ์ด ๋ชฉ์
์์ฉ ํ๋ก๊ทธ๋จ์ ํ์ค ๊ตฌ์กฐ ๋ฅผ ๊ตฌํํ๋ class์ library์ ๋ชฉ๋ก
Why do we have to learn django?
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 up-to-date documentation.
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
Instagram
Dropbox
Delivery Hero
etc.
์ฐ๋ฆฌ๊ฐ ์๊ณ ์๋ web ์ protocol
์์ฒญ (request)
์๋ต (response)
django ๋ ์ด๋ป๊ฒ (how) ๋์ํ๋๊ฐ?
django๋ MVC (Model View Controller) pattern ์ ๋ฐ๋ฅด๊ณ ์๋ค.
MVC: Software design pattern ์ค ํ๋
MVC PatterndjangoModel
Model
View
Template
Controller
View
MTV
M: ๋ฐ์ดํฐ ๊ด๋ฆฌ
T: ์ธํฐํ์ด์ค (ํ๋ฉด)
V: ์ค๊ฐ ๊ด๋ฆฌ (์ํธ ๋์)

Run django on CS 50 IDE
CS 50 IDE
: CS50 IDE is a cloud-based Integrated Development Environment powered by AWS Cloud9 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!
AWS Cloud 9
Cloud ๊ธฐ๋ฐ IDE (ํตํฉ ๊ฐ๋ฐ ํ๊ฒฝ)
OS: Ubuntu 18.04.4 LTS
python 3.7.6
๊ฐ๋ฐ local ํ๊ฒฝ์ ์ํฅ์ด ์๋ค๋ ์ฅ์ ์ด ์์

django ์ค์น
$ pip install django==2.1.15
2.1.15 ๋ก ๋ค์ด๋ฐ๊ธฐ
django uninstall
$ pip uninstall django
ํ๋ก์ ํธ ์์ฑ
$ django-admin startproject {ํ๋ก์ ํธ ๋ช
}
์๋ฒ ์คํ
settings.py 28 ๋ฒ์งธ ์ค ์์ ํ๊ธฐ
ALLOWED_HOSTS = ['*']
์๋ฒ ์คํ ์ ๋ฐ๋์ ๋ช ๋ น์ด๊ฐ ์คํ๋๋ directory๋ฅผ ํ์ธ ํ ๊ฒ
$ cd djanggo_intro/
$ ls
$ python manage.py runserver 8080
์คํ๋ ์๋ฒ๋ ์ฐ์ธก ์์ญ์ url์ clickํ๋ค
image-20200326102907761 ์๋ฒ ์ข ๋ฃ๋ terminal์์
ctrl + c
์๋ฒ ์คํ ๋ ํ๋ฉด

app ๋ง๋ค๊ธฐ
$ python manage.py startapp pages
Project ์์ํ๊ธฐ
$ django-admin startproject first_django
project
๋ django์ atomic ํ ๋จ์!
์์ฑ๋ project folder ๊ตฌ์กฐ

Pages ๋ง๋ค๊ธฐ
$ python manage.py startapp pages
Git
$ git init
$ ls -al
$ rm -rf .git
INSTALLED_APPS
INSTALLED_APPS
: ์ฃผ๋ฏผ๋ฑ๋ก

์ฃผ๋ฏผ๋ฑ๋ก์ ์ํด
pages
์ถ๊ฐํจ
Server ๊ฐ๋
$ python manage.py runserver 8080

Port ๋ฒํธ ํ์ธ
$ echo $PORT
8080
80๋ฒ๊ณผ 8080์ผ๋ก ์ฐ๊ฒฐ๋์ด ์๋ ๊ฒ ํ์ธ ๊ฐ๋ฅ
IP ํ์ธ
$ echo $IP
127.0.0.1
django๋ DEBUG=True
๊ฐ default
urls.py
๋ฌธ์ง๊ธฐ
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('์์ฒญ URL/', handling ํ view(controller) ํจ์)
path('admin/', admin.site.urls),
]
path('์์ฒญ URL/', handling ํ view(controller) ํจ์)
url ๋์
/
๊ฐ ๋ถ๋๊ฒ django์ ํน์ง!
pages/views.py
from django.shortcuts import render
# Create your views here.
def hello():
return render(request,'HTML file name')
hello() ๊ฐ ๋ถ๋ฆด ๋๋ง๋ค ํจ์๋ฅผ ๋ถ๋ฅธ ๊ฐ์ฒด๊ฐ ์ฒซ๋ฒ์งธ ์ธ์๋ก ๋ค์ด๊ฐ
view์์ ํจ์๋ฅผ ์ ์ํ๋ ๊ฒฝ์ฐ ํญ์ ์ฒซ๋ฒ์งธ ์ธ์๋ฅผ
request
๋ก ์ ์ํ๋ค!
def lotto(request):
import random
pick = random.sample(range(1,46),6)
context = {'pick':pick}
return render(request, 'lotto.html', context)
์ฒซ ๋ฒ์งธ ์ธ์ -
request
๋ ๋ฒ์งธ ์ธ์ - ํ์ผ ๋ช
์ธ ๋ฒ์งธ ์ธ์ - context
context๋ ํญ์ dictionary ๋ก ํด์ผํจ!
pages/templates folder ๋ง๋ค๊ธฐ
์ด๋ฆ์ด ๊ผญ
templates
์ฌ์ผ ํ๋ค
๋๊ณ ์๋ python ํ์ผ ์ฃฝ์ด๊ธฐ
$ killall python
$ killall python3
Wrap-up
App ์์ฑ
django๋ ์ฌ๋ฌ๊ฐ์ ์ฑ์ ๊ฐ์ง ํ๋์ project๋ก ๊ตฌ์ฑ๋๋ค
ex) ์ปค๋ฎค๋ํฐ๋ฅผ ๋ง๋ ๋ค
ํ์๊ณผ ๊ด๋ จ๋ app -
accounts
๊ฒ์๊ธ๊ณผ ๊ด๋ จ๋ app -
posts
$ python manage.py startapp {app ์ด๋ฆ}
app์ ์์ฑํ๊ณ ๋ฐ๋์
settings.py
์INSTALLED_APPS
์ ๋ฑ๋กํ๋คINSTALLED_APS = [ ... 'pages', ]
๊ธฐ๋ณธ ํ๋ฆ
1. URL ์ง์
url.py
# django_intro/urls.py from pages import views urlpatters = [ path('lotto/', views.lotto), ]
path ์ url์ ํญ์
/
๋ก ๋ซ์์ค๋ค
2. view ์ง์
views.py
# pages/views.py import random def lotto(request): pick = random.sample(range(1,46),6) context = { 'pick': pick } return render(request, 'lotto.html', context)
ํจ์๋ฅผ ์ ์ํ ๋, ํญ์ ์ฒซ๋ฒ ์งธ ์ธ์๋
request
๋ก ์์ฑํด๋๋คwhy? ๋ด๋ถ์ ์ผ๋ก ์์ฒญ์ ์ฒ๋ฆฌ ํ ๋, ํจ์ ํธ์ถ ์ ์์ฒญ ์ ๋ณด๊ฐ ๋ด๊ธด ๊ฐ์ฒด๋ฅผ ๋ณด๋ด์ค์ผ ํด์
render
ํจ์๋ฅผ ํตํด์ ๋ฐํํ๋ค์ฒซ๋ฒ์งธ ์ธ์:
reqeust
๋ ๋ฒ์งธ ์ธ์: template file (
html
)์ธ๋ฒ์งธ ์ธ์
dictionary type
template์์ ํ์ฉ ํ๋ ค๊ณ ํ๋ ๊ฐ๋ค์ ์ ๋ฌ
3. templates
ํ์ผ ์์ฑ
templates
ํ์ผ ์์ฑ๋ฐํํ
html
file์ ํญ์templates
folder ์์ ์์ฑํ๋ค<!-- pages/templates/lotto.html --> <p> {{pick}} </p>
context dictionary์ key ๊ฐ์ ์ ์ผ๋ฉด ์ถ๋ ฅ๋๋ค
Last updated
Was this helpful?