Django tutorial, part 1

김수민·2020년 5월 3일
0

Django+uWSGI+nginx

목록 보기
2/9

Django란?

https://www.djangoproject.com/

"Django makes it easier to build better Web apps more quickly and with less code."

  • Django는 high-level Python Web Framework이다.
  • 빠르게 개발할 수 있다.
  • 보안 지원
  • 다양한 확장

Django 설치

https://docs.djangoproject.com/en/3.0/intro/install/

첫번째 Django Project 생성

https://docs.djangoproject.com/en/3.0/intro/tutorial01/

django-admin startproject <mysite>
python manage.py runserver 0:8000

Polls app 생성

Django에서는 project와 app의 개념이 있다.
project는 특정 website에 대한 config와 app들의 집합이다.
app은 특정 Web application을 말한다.
하나의 app이 여러 project를 가질 수도 있고 하나의 project가 여러 app을 가질 수 도 있다.

위에서 생성한 project에 하나의 app을 추가로 생성한다.
python manage.py startapp polls

├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── settings.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── wsgi.cpython-36.pyc
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── polls
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

첫번째 view 작성

// polls.views.py
from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

작성한 view를 호출하기 위해서는 URLconf 작업을 해주어야 한다.

// polls/urls.py
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

root URLconf에 polls.urls 모듈을 추가하는 작업을 해주어야 한다.

//mysite/urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]
// include() 함수는 다른 URLconf 모듈을 참조할 수 있도록 해준다.
// admin.site.urls만 예외이다.

// path() 함수는 4개의 인자를 가진다.
// route와 view는 필수이며, kwargs와 name은 option이다.

profile
python developer

0개의 댓글