Raspberry PiでDjango+mariaDB
はじめに
Raspberry PiでDjangoを動かしてみます。DBは、mariaDBで。
前提
- Raspberry Pi 3 Model B
- raspbian
- MariaDB
- Python 3.7.3
- とりあえず外部からアクセスしたい
djangoをインストールする
$ sudo pip3 install django
プロジェクトを作成する
sampleというプロジェクトを作成することにしています。
$ django-admin.py startproject sample
アプリを作成する
sampleappというアプリを作成することにしています。
$ cd sample $ django-admin.py startapp sampleapp
setting.pyを編集する
sample/sample/setting.pyを編集します。
ALLOWED_HOSTSは、外部アクセスさせたいので * を記述します。
DATABASESは、mariadbの定義に合わせて記述します。
ALLOWED_HOSTS = ['*'] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'hoge', # DB名 'USER': 'user', # ユーザ名 'HOST': 'localhost', 'PASSWORD': 'password', 'PORT': '3306', } }
データベースを構築する
$ sudo python3 manage.py migrate 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
データベース管理者を設定する
$ sudo python3 manage.py createsuperuser Username (leave blank to use 'root'): Email address: Password: Password (again): Superuser created successfully.
起動する
$ sudo python3 manage.py runserver 0.0.0.0:8000
確認する
ブラウザで、URLを入力します。(http://xxx.xxx.xxx.xxx:8000)