갈루아의 반서재

예제로 투표 시스템을 만들어 본다.


2가지 부분으로 구성이 되는데,

1. 사용자가 개설된 투표 목록을 보고 투표 행위를 하는 부분과

2. 투표를 추가/변경/삭제하는 관리자 페이지 부분이다.


※ 참고 : 예제는 Django 1.8 과 Python 2.7 버전 기준이다.


Creating a project 

Django가 처음이라면 Django 프로젝트를 생성하는 몇 가지 코드를 실행해야 한다.


(venv)root@seoul:~# django-admin startproject mysite


생성된 모습을 확인해보자.

mysite/

a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.

  manage.py

A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.

  mysite/

actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).

   __init__.py

An empty file that tells Python that this directory should be considered a Python package. (Read more about packages in the official Python docs if you’re a Python beginner.)

   settings.py

Settings/configuration for this Django project. Django settings will tell you all about how settings work.

   urls.py

The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.

   wsgi.py

An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

 

Database setup 

파이썬에는 SQLite 가 포함되어 있으므로 별도의 데이터베이스를 설치할 필요는 없다.

다른 데이터베이스를 사용하고 싶다면 아래의 셋팅값을 수정한다.


# Database
https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


•ENGINE

'django.db.backends.sqlite3'

'django.db.backends.postgresql_psycopg2'

'django.db.backends.mysql'

'django.db.backends.oracle'


•NAME : 데이터베이스 이름

만약 SQLite 를 사용중이라면 데이터베이스는 파일이 될 것이다.

데이터베이스의 이름은 파일 이름을 포함하여 절대 경로가 된다. 


USER, PASSWORD, HOST 등의 추가적인 정보는 아래와 같이 입력한다.

더 상세한 내용은 DATABASES 문서 참조.


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}


INSTALLED_APPS 셋팅은 해당 Django 인스턴스에서 활성화된 모든 Django 어플리케이션의 이름이다.

앱은 여러개의 프로젝트에서 사용될 수 있고 다른 프로젝트에서 활용하기 위해 패키지화하거나 배포할 수 있다.


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)


일부 어플리케이션의 최소한 한 개 이상의 테이블을 요구하므로, 사용하기전에 먼저 테이블부터 생성해야 한다.

아래와 같이 실행한다.


$ python manage.py migrate




mysite/settings.py 파일에 포함된 데이터베이스 셋팅에 따라 필요한 테이블을 생성한다.


The development server

현재까지 셋팅된 값을 가지고 서버를 실행해보자

로컬이 아니라 호스팅을 받고 있다면 해당 IP 를 입력하여 실행한다.

자세한 자료는 https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-runserver 참조.


(venv)root@gcloud-seoul-f461285987ddc0629f8a4ab6477642b5:~/mysite# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
June 15, 2015 - 03:29:34
Django version 1.8.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.





위의 '해당 웹페이지를 사용할 수 없음'이라고 표시되는 것은 접속한 곳이 서버 내부가 아닌 외부이기 때문이다.

이 경우 아래와 같이 설행한다.

(venv)root@gcloud-seoul-f461285987ddc0629f8a4ab6477642b5:~/mysite# python manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
June 15, 2015 - 04:11:40
Django version 1.8.2, using settings 'mysite.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[15/Jun/2015 04:11:42]"GET / HTTP/1.1" 200 1767