Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

Contents

왜 Django

그동안 웹 애플리케이션은 Ruby sinatra로 개발해왔다. 루비쪽은 ROR이 워낙에 유명하긴 하지만 (나는)웹 애플리케이션 전문적으로 개발하진 않는다. 필요할 때, 프로토타이핑이나 POC 정도 하는게 목적이라서 가볍고 사용하기 편한 프레임워크를 찾다보니 ROR이 아닌 sinatra를 선택하게 됐다.

그러다가 django를 해야 하는 상황에 처했다. 이제와서 python 기반의 웹 프레임워크를 그것도 애초 목적에 맞지도 않는 풀 프레임워크인 django를 공부하려 하느냐라고 물을 수도 있겠다. 이유를 설명하자면.. 나도 django 하기 싫다. 그런데 회사 프로젝트가 django 기반이라서 하는거다.

Django 간단 소개

Django는 파이선 기반의 웹 프레임워크로 실용적인 디자인을 바탕으로 유려한 모양의 웹 애플리케이션을 빠르게 개발할 수 있도록 도와준다.

Django 설치

환경

  • 하이퍼바이저 : virtualbox 4.2.16
  • 운영체제 : Ubuntu 리눅스 13.04 server
  • python 버전 : 2.7.5+

설치

먼저 pip를 설치한다. pip는 python 패키지 관리자로 루비의 gem쯤 되는 놈이다.
# apt-get install python-pip

pip를 이용해서 django를 설치한다.
# pip install django
Downloading/unpacking django
  Downloading Django-1.5.5.tar.gz (8.1MB): 8.1MB downloaded
  Running setup.py egg_info for package django
...

Django 애플리케이션 개발

Django 와 MVC

MVC 패턴 참조

Django 애플리케이션 서버 실행

웹 애플리케이션을 배포하기 위해서 "/opt/webapp" 디렉토리를 만들고 django-admin을 이용해서 "testapp" 프로젝트를 만들었다.
# mkdir /opt/webapp
# cd /opt/webapp
# django-admin.py startproject testapp 

testapp 디렉토리가 만들어졌다. 디렉토리 구조는 다음과 같다.
# tree testapp/
testapp/
├── testapp
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

testapp을 실행했다.
# python manage.py runserver
Validating models...

0 errors found
November 02, 2013 - 05:53:27
Django version 1.5.5, using settings 'helloworld.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
웹브라우저로 테스트를 해보자.

https://lh5.googleusercontent.com/-6bUULF0T-rY/UnTag20Nv1I/AAAAAAAADWg/BI-QxzknzVE/s640/django01.png

Hello World

Django 서버를 실행했으니, 이제 웹 애플리케이션을 개발해보려 한다. 첫번째 애플리케이션은 helloworld앱으로 브라우저로 /hello을 요청하면 Hello world!!!를 출력하는 간단한 일을 한다.

먼저 view 파일을 만들자.
# cat views.py
from django.http import HttpResponse
def hello(request):
        return HttpResponse("Hello world!!!")

이제 urls.py를 수정한다. 핵심은 브라우저가 /hello를 요청하면 views의 hello 메서드를 호출하는 거다.
# cat urls.py
from django.conf.urls import patterns, include, url

from testapp.views import hello
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
        url(r'^hello/$', hello)
)
서버를 띄우고 브라우저로 localhost:8000/hello를 요청하면 "Hello world!!!"가 출력되는 걸 확인할 수 있다.

현재 루트디렉토리(localhost:8000/)를 요청하면 404 NOT Found가 출력된다. 루트디렉토리에 대한 요청도 처리하기를 원한다면 다음과 같이 url을 추가하면 된다. url패턴은 python 정규표현식을 따른다.
# cat urls.py
from django.conf.urls import patterns, include, url

from testapp.views import hello, index
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
        url(r'^$', index),
        url(r'^hello/$', hello)
)

물론 view에 index 메서드는 추가해줘야 한다.
from django.http import HttpResponse
def hello(request):
        return HttpResponse("Hello world")
def index(request):
        return HttpResponse("Home page")

URLconf 설정

Hello world예제를 만들어봤더니, 대략의 소프트웨어 개발 패턴이 눈에 띈다. URLconf를 이용해서 URL과 메서드를 느슨하게 결합했다는 점이다. URL은 지시라고 볼 수 있는데, 지시코드를 느슨하게 결합함으로써, 다른 지시 혹은 코드에 영향을 주지 않는 소프트웨어의 개발이 가능하다.

Hello world 예제를 확장해 보자. 현재 시간을 알려주는 기능을 추가하기로 했다.
# cat views.py 
import datetime
from django.http import HttpResponse
def hello(request):
        return HttpResponse("Hello world")
def index(request):
        return HttpResponse("Home page")
def currenttime(request):
        d = datetime.datetime.now()
        return HttpResponse("Current Time : {0}".format(d))

# cat
from django.conf.urls import patterns, include, url
from testapp.views import hello, index, currenttime

urlpatterns = patterns('',
        url(r'^$', index),
        url(r'^time/$', currenttime),
        url(r'^hello/$', hello)
)

참고