Intro to Django

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?

  1. Portable

    • Python based which means it runs on may platform

  2. 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.

  3. 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)

  4. 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

  5. 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

  6. Maintainable

    • Follows DRY (Don't Repeat Yourself) principle

      • No unnecessary duplication

    • Promotes the grouping of related functionality into reusable

  7. ๋งŽ์€ ๊ธฐ์—…์—์„œ ํ™œ์šฉ์ค‘

    • Spotify

    • Instagram

    • Dropbox

    • Delivery Hero

    • etc.

์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” web ์˜ protocol

  • ์š”์ฒญ (request)

  • ์‘๋‹ต (response)

django ๋Š” ์–ด๋–ป๊ฒŒ (how) ๋™์ž‘ํ•˜๋Š”๊ฐ€?

  • django๋Š” MVC (Model View Controller) pattern ์„ ๋”ฐ๋ฅด๊ณ  ์žˆ๋‹ค.

    • MVC: Software design pattern ์ค‘ ํ•˜๋‚˜

      MVC Pattern

      django

      Model

      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ํ•œ๋‹ค

  • ์„œ๋ฒ„ ์ข…๋ฃŒ๋Š” 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

: ์ฃผ๋ฏผ๋“ฑ๋ก

  • ์ฃผ๋ฏผ๋“ฑ๋ก์„ ์œ„ํ•ด 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 ํŒŒ์ผ ์ƒ์„ฑ

  • ๋ฐ˜ํ™˜ํ•  html file์€ ํ•ญ์ƒ templates folder ์•ˆ์— ์ƒ์„ฑํ•œ๋‹ค

    <!-- pages/templates/lotto.html -->
    <p>
        {{pick}}
    </p>
    • context dictionary์˜ key ๊ฐ’์„ ์ ์œผ๋ฉด ์ถœ๋ ฅ๋œ๋‹ค

Last updated