갈루아의 반서재

아나콘다를 통해 설치되는 버전은 django 2.0.5 이다. 금일 기준 최신 공식버전은 2.0.6 이다 (https://www.djangoproject.com/download/).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(redsparrow) fukaerii@server:~$ conda install -c anaconda django
Solving environment: done
 
## Package Plan ##
 
  environment location: /home/fukaerii/anaconda3/envs/redsparrow
 
  added / updated specs:
    - django
 
 
The following packages will be downloaded:
 
    package                    |            build
    ---------------------------|-----------------
    certifi-2018.4.16          |           py36_0         142 KB  anaconda
    django-2.0.5               |   py36hd476221_0         4.7 MB  anaconda
    openssl-1.0.2o             |       h20670df_0         3.4 MB  anaconda
    pytz-2018.4                |           py36_0         212 KB  anaconda
    ca-certificates-2018.03.07 |                0         124 KB  anaconda
    ------------------------------------------------------------
                                           Total:         8.6 MB
 
The following NEW packages will be INSTALLED:
 
    django:          2.0.5-py36hd476221_0 anaconda
    pytz:            2018.4-py36_0        anaconda
 
The following packages will be UPDATED:
 
    ca-certificates: 2018.03.07-0                  --> 2018.03.07-0      anaconda
    certifi:         2018.4.16-py36_0              --> 2018.4.16-py36_0  anaconda
    openssl:         1.0.2o-h20670df_0             --> 1.0.2o-h20670df_0 anaconda
 
Proceed ([y]/n)?
 
cs


파이썬을 통해 Django 설치가 정상적으로 이루어졌는지 검증한다.

1
2
3
4
5
6
7
8
9
(redsparrow) fukaerii@server:~$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 201816:14:56)
[GCC 7.2.0] on linux
Type "help""copyright""credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
2.0.5
>>>
 
cs

아래와 같이 간단하게도 가능하다

1
2
(redsparrow) fukaerii@server:~$ django-admin --version
2.0.5
cs


Django가 설치된 경로는 다음과 같다.

/home/fukaerii/anaconda3/envs/redsparrow/lib/python3.6/site-packages/django


그러면 프로젝트를 만들어보자.

django-admin 을 통해 어플리케이션을 생성할 수 있다(/home/fukaerii/anaconda3/envs/redsparrow/lib/python3.6/site-packages/django/bin/django-admin.py).

startproject 명령을 통해 프로젝트 디렉토리 구조를 만든다.

다음의 명령을 실행해보자. django-admin startproject <projectname> 명령은 프로젝트 디렉토리와 프로젝트 패키지 이름을 <projectname> 으로 명명하게 되고, 명령이 실행된 디렉토리에 프로젝트를 생성하게 된다. 

만약 옵션인 <destination> 파라메터를 넣게되면, Django 는 제공된 목적지 디렉토리를 프로젝트 디렉토리로 사용하게 되고, 그 디렉토리 안에 manage.py 과 프로젝트 패키지 파일을 생성한다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(redsparrow) fukaerii@server:~$ django-admin startproject dominika
(redsparrow) fukaerii@server:~$ ls -al
total 48
drwxr-xr-x  5 fukaerii fukaerii 4096 Jun 15 16:44 .
drwxr-xr-x  3 root     root     4096 Jun 14 12:24 ..
drwxrwxr-23 fukaerii fukaerii 4096 Jun 14 16:56 anaconda3
-rw-------  1 fukaerii fukaerii   36 Jun 14 16:46 .bash_history
-rw-r--r--  1 fukaerii fukaerii  220 Jun 14 12:24 .bash_logout
-rw-r--r--  1 fukaerii fukaerii 3852 Jun 14 16:56 .bashrc
-rw-r--r--  1 fukaerii fukaerii 3771 Jun 14 16:56 .bashrc-anaconda3.bak
drwxrwxr-x  3 fukaerii fukaerii 4096 Jun 14 16:58 .conda
drwxrwxr-x  3 fukaerii fukaerii 4096 Jun 15 16:44 dominika
-rw-rw-r--  1 fukaerii fukaerii  825 Jun 14 18:04 environment.yaml
-rw-r--r--  1 fukaerii fukaerii  807 Jun 14 12:24 .profile
-rw-------  1 fukaerii fukaerii   54 Jun 15 16:31 .python_history
(redsparrow) fukaerii@server:~$ pwd
/home/fukaerii
cs


그러면 어떤 식으로 프로젝트 파일들이 생성되는지 살펴보자. 해당 디렉토리에는 manage.py 파일과 dominika 라는 폴더가 있다. 여기서 manage.py 파일은 django-admin 과 유사한 파일로 프로젝트 패키지를 sys.path 에 올린다. 그리고 DJANGO_SETTINGS_MODULE 환경변수가 생성된 프로젝트의 settings.pyfile 을 가리키도록 하는 역할도 한다.


여기서 manage.py 파일의 내용을 살펴보자. 나올 때는 q 를 누르면 된다.

1
(redsparrow) fukaerii@server:~$ less manage.py
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
#!/usr/bin/env python
import os
import sys
 
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE""dominika.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)
manage.py (END)
 
cs


dominika 폴더 아래에는 다음과 같이 4개의 파일이 있음을 알 수 있다. 


여기에는 __init__.py settings.py urls.py wsgi.py 이렇게 4개의 파일이 있는데 각각의 용도는 다음과 같다. 

__init__.py 는 여러분의 파이썬 프로젝트의 시작점을 나타낸다.

settings.py 는 Django 설치 구성에 대해 언급하고, Django 로 하여금 어느 셋팅이 유효한지 알려준다.

urls.py 는 라우팅과 URL을 각각의 views로 매핑하는 기능을 가진 urlpatterns 리스트를 가지고 있다. list, that routes and maps URLs to their .

wsgi.py 는 웹서버 게이트웨이 인터페이스에 대한 설정을 담고 있다. Web Server Gateway Interface (WSGI) 는 웹서버와 어플리케이션을 배포하는 파이썬 플랫폼 표준이다. 


runserver 명령을 통해 서버를 구동한 후 구현된 웹사이트를 눈으로 확인해보자.

서버 ip 주소를 /home/fukaerii/dominika/dominika/ 경로 상의 settings.py 을 열어  ALLOWED_HOSTS 에 등록해야 한다. 

Django 문서에 따르면, ALLOWED_HOSTS 변수는 Django 사이트가 서브할 수 있는 호스트/도메인 이름을 나타내는 문자열의 집합이다. 이는 HTTP 호스트 헤더 공격을 막기 위한 보안 방법 중 하나이다. 

해당 파일의 Allowed Hosts 란에 서버 IP 를 입력해주면 된다. 그리고 저장 후 빠져나온다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
 
ALLOWED_HOSTS = ['***.***.**.**']
 
 
# Application definition
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
cs


다시 manage.py 파일이 위치한 디렉토리로 이동하여 아래의 명령을 실행한다. 여기서 ***.***.**.** 는 앞서 입력한 여러분의 서버 IP 주소이다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
(redsparrow) fukaerii@server:~/dominika$ python manage.py runserver ***.***.**.**:8000
Performing system checks...
 
System check identified no issues (0 silenced).
 
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
 
June 162018 - 02:43:45
Django version 2.0.5, using settings 'dominika.settings'
Starting development server at http://***.***.**.**:8000/
Quit the server with CONTROL-C.
 
 
cs


브라우저를 열고 http://***.***.***.**:8000/ 을 주소창에 넣으면 다음과 같이 완성된 화면을 볼 수 있다.