Django(3) Djangoからデータベースを弄ってみよう
Sponsored Links
皆さんこんにちは
お元気ですか?ええ、数学って難しいですよね
さて、久しぶりにDjangoについて
前回の記事は以下
Django(2) アプリケーションを作ろう。 - のんびりしているエンジニアの日記
今回はデータベースを触ってみよう
まずsettings.pyを編集します
DATABASEの欄があるので、自分の設定しやすいように変更してください
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'DB名', 'USER': 'ユーザー名', 'PASSWORD': 'パス', 'HOST': 'localhost', # Or an IP Address that your DB is hosted on 'PORT': '22', } }
次にMysqlを起動し、databaseを作ります。
create database [db名]
最後にmanage.pyのある場所でコマンドを動作させます。
因みにpythonでmysqlを動作させるライブラリが入っていない人は以下のコマンドで先に入れてください。
sudo pip install mysql-python
python manage.py syncdb Creating tables ... Creating table django_admin_log Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_groups Creating table auth_user_user_permissions Creating table auth_user Creating table django_content_type Creating table django_session You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): yes Username (leave blank to use 'tereka'): Email address: Password: Password (again): Superuser created successfully. Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s)
基本的に設定は不要です。