Ossian Story
article thumbnail

 


[ Django 설치 전 가상환경 실행 ]

#workon [ 가상환경 이름 ]

$ workon env_django

 


[ Django 설치 ]

  • Django 최신 버전 설치
$ pip install django

 

  • Django 특정 버전 설치
$ pip install django==2.2.7

 


[ Django Project  생성 ]

  • Django Project를 생성할 디렉토리로 이동
 $ cd /opt

 

  • Django Project 생성
# django-admin startproject [ Project Name ]

$ django-admin startproject todo

 

  • 생성된 Django Project 확인
.
└── todo
    ├── manage.py
    └── todo
        ├── __init__.py
        ├── asgi.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

[ Django Project 테스트 설정 ]

  • 생성된 프로젝트의 setting.py 파일 수정
    - [ DATABASES ]는 Django에서 사용할 DATABASE를 설정할 수 있으며 기본적으로 sqlite3를 사용
...

ALLOWED_HOSTS = ['*'] # 모든 호스트에서 접근될 수 있도록 설정 변경

...

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

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

 


[ Django Project 실행 테스트 ]

  • manage.py 파일이 위치한 Django Project 디렉토리에서 runserver 0.0.0.0:80 실행
# runserver [ HOST IP ]:[ Port ]

$ ./manage.py runserver 0.0.0.0:80

※ Django 2.1 버전 이하는 sqlite3 3.7로 실행가능

※ Django 2.2 버전 이상은 sqlite3 3.8 이상으로 실행가능

 

  • Django 2.2 버전 이상에서 sqlite3 실행 시 오류 발생
ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).

 


[ sqlite3 3.8 이상 업데이트 방법 ]

  • sqlite3 최신 버전 다운로드 및 설치
# https://www.sqlite.org/{year}/{sqlite-autoconf-{version}.tar.gz)

$ wget https://www.sqlite.org/2020/sqlite-autoconf-3310100.tar.gz

$ yum -y install gcc
$ tar zxvf sqlite-autoconf-3310100.tar.gz
$ cd sqlite-autoconf-3310100
$ ./configure --prefix=/usr/local
$ make
$ sudo make install

 

  • 컴파일 후 시스템 링크 라이브러리에 반영
$ echo "/usr/local/lib" >> /etc/ld.so.conf
$ /sbin/ldconfig

 

  • 설치한 sqlite3 반영
$ vi .bashrc

...
#SQLITE3 Setting
export LD_LIBRARY_PATH=/usr/local/lib

 

  • 변경된 Bashrc Profile 적용
$ source .bashrc

 

  • Django Project 디렉토리로 이동하여 runserver 0.0.0.0:80 실행
$ ./manage.py runserver 0.0.0.0:80

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 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.

February 02, 2020 - 12:09:49
Django version 3.0.2, using settings 'todo.settings'
Starting development server at http://0.0.0.0:80/
Quit the server with CONTROL-C.

 


[ Django sqlite3 Database에 관리자 생성 ]

  • runserver 실행 후 sqlite3 Database 생성 확인
$ tree

.
├── db.sqlite3  # 생성된 sqlite3 Database
├── manage.py
└── todo
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-36.pyc
    │   ├── settings.cpython-36.pyc
    │   ├── urls.cpython-36.pyc
    │   └── wsgi.cpython-36.pyc
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

 

  • sqlite3에 Django Project Table 생성 및 반영
$ ./manage.py makemigrations #sqlite3 Database에 반영할 sql 명령어 생성
No changes detected

$ ./manage.py migrate # makemigrations을 통해 생성된 sql 명령어를 실행하여 Database에 적용
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

 

  • 관리자 계정 생성
$ ./manage.py createsuperuser

Username (leave blank to use 'root'): admin
Email address: admin@admin.com
Password:
Password (again):
Superuser created successfully.

 


[ Django Project Dev Web Service 시작 ]

  • runserver 0.0.0.0:80 실행 및 브라우저에서 접속
$ ./manage.py runserver 0.0.0.0:80

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
February 02, 2020 - 12:17:05
Django version 3.0.2, using settings 'todo.settings'
Starting development server at http://0.0.0.0:80/
Quit the server with CONTROL-C.

 

  • http://[ Django Project Server IP ]

 

  • http://[ Django Project Server IP]/admin

 


 

profile

Ossian Story

@ossians