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. 결과

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

1. 프로젝트 생성

cd /root
mkdir django
cd django
cp /usr/lib/python2.7/dist-packages/django/bin/django-admin.py .
python django-admin.py startproject FIRST

 → 위의 명령어을 정상적으로 수행했다면 아래와 같은 구조의 디렉토리가 생셩되게 됩니다.

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

 

2. DB생성

root@ubuntu:~/django# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.29-0ubuntu0.12.04.2 (Ubuntu)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database board;

 

3. DB 및 환경설정

root@ubuntu:~/django/FIRST# vi settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',         # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'board',                                        # Or path to database file if using sqlite3.
        'USER': 'root',                                           # Not used with sqlite3.
        'PASSWORD': '설정하고싶은 비밀번호',      # Not used with sqlite3.
        'HOST': 'localhost',                                   # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '3306',                                          # Set to empty string for default. Not used with sqlite3.
    }
}

TIME_ZONE = 'Asia/Seoul'

→ 위와 같이 DB정보와 함께 TIME_ZONE을 설정하도록 합니다.

 

4. DB 동기화

root@ubuntu02:~/django/FIRST# python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site

 

5. 서비스 시작

root@ubuntu:~/django/FIRST# python manage.py runserver 0.0.0.0:80
Validating models...

0 errors found
Django version 1.3.1, using settings 'board.settings'
Development server is running at http://0.0.0.0:80/
Quit the server with CONTROL-C.

 

6. 서비스 확인

위의 사진이 보인다면 정상적으로 세팅이 된것입니다. 안되었다면 연락주세요..

'DJANGO' 카테고리의 다른 글

DJANGO #6. Template 적용하기  (0) 2013.05.17
DJANGO #5. 어플생성 및 기본 작동 방법 설명  (0) 2013.04.05
DJANGO #3. Python, Django 설치  (0) 2013.04.03
DJANGO #2. Tutorial 설명  (0) 2013.04.03
DJANGO #1. Django 정의 및 특징  (0) 2013.04.03

1. Tutorial OS

우분투 12.04 Desktop 버젼 : 해당 버젼의 설치 가이드는 추후 제공할 예정입니다.

2. Python 설치

해당 버젼에는 Python 2.7 버젼이 기본 설치되어 있습니다.

Python 설치를 확인하는 방법은

root@ubuntu01:/# python -V(대문자)

Python 2.7.3

위와 같이 표시가 되면 정상적으로 설치된 것이라고 볼수 있습니다.

2. Django 설치

3. Mysql 설치

4. MySQLDB 설치

5. easy_install 설치


일단 이것은 나중에 쓰겠습니다. 여기까지는 인터넷에 많습니다. 이 다음이 없어서

조금 순서에 맞지는 않지만 다음 장 먼저 쓰고 이번 장을 쓰도록 하겠습니다. 


+ Recent posts