1. 프로젝트 아래 APP만들기

root@ubuntu:~/django/FIRST# python manage.py startapp board

→ 이전에 우리는 FIRST라는 프로젝트를 생성했는데 이제 그 아래 board라는 어플리케이션을 만듭니다. 해당 어플리케이션이 정상적으로 생셩이 되었다면 아래와 같은 구조의 디렉토리와 파일들이 자동적으로 생성될 것입니다.

root@ubuntu:~/django# ls -R FIRST
FIRST:
board  __init__.py  __init__.pyc  manage.py  settings.py  settings.pyc  urls.py

FIRST/board:
__init__.py  models.py  tests.py  views.py

root@ubuntu:~/django#

→ 이제 우리는 위의 파일들 중 FIRST/urls.py, FIRST/board/views.py를 이용하여 주소가 있는 페이지를 만들어 볼것입니다.

 

2. urls.py 편집

from django.conf.urls.defaults import patterns, include, url

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

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'FIRST.views.home', name='home'),
    # url(r'^FIRST/', include('FIRST.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
)

처음 기본적으로 Django를 설치하면 위의 파일은 지금 보는 것처럼 설정되어 있습니다. urls.py를 간단히 설명하면

우리가 입력하는 주소를 누가 처리할지를 정해주는 곳이라고 보면됩니다. (JAVA의 DISPATCHER라고나 할까요?)

 

위의 주소를 아래과 같이 바꾸어봅니다.

from django.conf.urls.defaults import patterns, include, url

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

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'FIRST.views.home', name='home'),
    # url(r'^FIRST/', include('FIRST.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'board.views.index'),
)

위의 라인에 대해 설명을 드리자면 설명을 드리지 않겠습니다. 17라인에 대해 설명을 드리면 r'^$'이라는 부분은 주소를 나타내는 마지막 / 뒤에 아무것도 붙지 않으면 즉 '/' 입력을 나타내는 것입니다. 

만약 주소로 0.0.0.0/ 을 입력한다면 이전에 우리가 만들었던 board/views.py의 index 메소드에서 처리한 결과를 출력한다 

 

3. /board/views.py 편집

# Create your views here.

from django.http import HttpResponse

def index(request):
        return HttpResponse('HELLO')

위의 라인에 대해서 설명을 드리면 request를 입력으로 받는 index메소드는 HttpResponse('HELLO')를 리턴한다 입니다.

나를 부르면 나는 웹에다가 HELLO를 찍겠다.

 

4. 결과

 위의 결과가 나온다면 정상입니다 .오늘은 여기까지 집에 가야지. ㅎㅎㅎ

+ Recent posts