[こう使って下さい!] NAVERクラウドプラットフォームでのジャンゴ(Django)の開発環境の構築及び活用[1]

NAVER CLOUD PLATFORM
NAVER CLOUD PLATFORM
13 min readJun 1, 2020

*このコンテンツは、NAVERクラウドプラットフォームTech Evangelistのソン・チャンアンさんが作成したものです。

こんにちは。NAVERクラウドプラットフォームです。

学びやすくて、色んな分野で活用できることから人気の高いプログラミング言語「Python(パイソン)」!

その中でもPythonベースのDjangoは、簡単かつ迅速にウェブサイトの開発ができるウェブフレームワークです!

今日は、NAVERクラウドプラットフォームのサーバーを用いて、Djangoの開発環境を構築する方法をご紹介します。

前書き..

Q. ジャンゴ(Django)は何ですか。

Djangoは、パイソン基盤で開発された無料オープンソースのウェブアプリケーションフレームワーク(web application framework)です。簡単かつ迅速なウェブサイトの開発ができるよう、サポートする構成要素からなるウェブフレームワークです。Djangoを使用すれば、多くのPythonライブラリーも使える上、パイソンの文法によって簡単かつ迅速に開発することができます。

サーバー構築及びパッケージ設置

0. NAVERクラウドプラットフォームのサーバーを構築します。

以前に、DNSとPUBLIC IPと連携された状態で進行する内容が含まれています。

  1. 構築したサーバーにパイソンパッケージ、pip関連のパイソンパッケージ及びパイソンsetuptoolsを設置します。
yum update python -yyum install python-setuptoolscurl -k -O https://bootstrap.pypa.io/get-pip.pypython get-pip.py

2. Django関連のパイソンパッケージを設置します。

pip install DjangoDEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting Django
Downloading https://files.pythonhosted.org/packages/cf/19/632a613bc37bbf890f9323ba09374ce9af1d70bb4cba7ff4d3e5e0991b47/Django-1.11.26-py2.py3-none-any.whl (6.9MB)
|████████████████████████████████| 7.0MB 745kB/s
Collecting pytz
Downloading https://files.pythonhosted.org/packages/e7/f9/f0b53f88060247251bf481fa6ea62cd0d25bf1b11a87888e53ce5b7c8ad2/pytz-2019.3-py2.py3-none-any.whl (509kB)
|████████████████████████████████| 512kB 38.6MB/s
Installing collected packages: pytz, Django
Successfully installed Django-1.11.26 pytz-2019.3

3. Djangoのバージョンを確認するために、下側のようにcommand shellに print(django.get_version())を入力します。すると、現在のバージョンが1.11.26であることが分かります。

# python 
Python 2.7.5 (default, Aug 7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5–39)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import django
>>> print(django.get_version())
1.11.26

4. Dajngoとmariadbを連結させるために、ホストにmariadbを設置します。

yum install -y mariadb mariadb-server mariadb-devel 
systemctl enable mariadb.service
systemctl start mariadb.service

5. Mysql関連のpythonパッケージを設置します。

pip install mysql-pythonDEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting mysql-python
Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Building wheels for collected packages: mysql-python
Building wheel for mysql-python (setup.py) ... done
Created wheel for mysql-python: filename=MySQL_python-1.2.5-cp27-cp27mu-linux_x86_64.whl size=84386 sha256=0d38521d6f79e9aa3f620ad88e1d9245001743e4d67ed421f1d9e0128640f60f
Stored in directory: /root/.cache/pip/wheels/07/d2/5f/314860e4cb53a44bf0ee0d051d4b34465e4b4fbe9de6d42f42
Successfully built mysql-python
Installing collected packages: mysql-python
Successfully installed mysql-python-1.2.5

6. Djangoの新しいプロジェクトをスタートします。現在の例題ではdasvaderに指定します。

django-admin.py startproject dasvader

7. 新しいプロジェクトは、dasvaderに基本的な設定を行います。

例題では、mariadbと連携及び外部ホスト接近時の許容リストを指定します。

vim dasvader/settings.pyDATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘djangotest’,
‘USER’: ‘csong’,
‘PASSWORD’: ‘djangotest’,
‘HOST’: ‘’,
‘PORT’: ‘’,
}
}
DATABASE_OPTIONS = {‘charset’: ‘utf8’}
LANGUAGE_CODE = ‘ko-kr’
TIME_ZONE = ‘Asia/Seoul’
….
ALLOWED_HOSTS = [“example.csong.kr”,”106.10.40.78"]

8. 設定が終わったら、python manage.py migrateの命令に従ってデータベースとsyncを始めます。

python manage.py migrateSystem check identified some issues:
WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection ‘default’
HINT: MySQL’s Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode
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 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 sessions.0001_initial… OK

9. プロジェクトにユーザーを作成し、adminページに接続できるよう指定します。

python manage.py createsuperuser
Username (leave blank to use ‘root’): admin
Email address: admin@csong.kr
Password: test1111
Password (again): test1111
Superuser created successfully.

10. プロジェクトを現在のテストサーバーに実行させ、外部から接続できるようにします。

python manage.py runserver 0.0.0.0:8000

11. 実行させたサーバーを外部からhttp://example.csong.kr:8000/に接続すると、最初のページに正常的に接続されることが分かります。

- 参考資料

https://tutorial.djangogirls.org/ko/django/

https://docs.djangoproject.com/en/1.11/intro/

http://pythonstudy.xyz/python/article/303-Django-%EC%84%A4%EC%B9%98

https://heiswed.tistory.com/entry/%EC%9E%A5%EA%B3%A0-%EA%B0%9C%EB%B0%9C-%ED%99%98%EA%B2%BD-%EB%A6%AC%EB%88%85%EC%8A%A4-%EC%9A%B4%EC%98%81%EC%84%9C%EB%B2%84%EC%97%90-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0

次回は..

今日は、NAVERクラウドプラットフォームのサービスを介してサーバーを構築し、サーバーにDjango関連パッケージを設置及び設定する方法について紹介しました。

次回は、Djangoを用いてRest APIサーバーを構築する方法について説明したいと思います。

より有益なコンテツをお楽しみに。

--

--

NAVER CLOUD PLATFORM
NAVER CLOUD PLATFORM

We provide cloud-based information technology services for industry leaders from startups to enterprises.