スポンサーリンク

【Django】Vagrantの環境でDjangoチュートリアルやってみた

チュートリアルページはこちら

まずはDjangoをインストールしとく

pipのインストールは

# pip install Django

インストール確認

[root@localhost feed_commenter_new]# pip list

Package          Version

—————– ——-

Django           2.1.7 

以下のようにバージョン指定もできるっぽい

# pip install Django=2.1

バージョン指定がなければpipでインストールできるバージョンの最新をもってくるのかな

はじめから進めていったのだが以下の項目で詰まった

・開発用サーバー

[root@localhost mysite]# python manage.py runserver 0:8000

※0はローカルホスト,localhostとやっても同じかな

起動してアクセス(http://192.168.33.10:8000/)してみると以下のメッセージ

——————————————————–

December 22, 2018 – 02:16:28

Django version 2.1.4, using settings ‘mysite.settings’

Starting development server at http://0:8000/

Quit the server with CONTROL-C.

Invalid HTTP_HOST header: ‘192.168.33.10:8000’. You may need to add ‘192.168.33.10’ to ALLOWED_HOSTS.

Bad Request: /


こちらのページを参考に修正

mysite/settings.pyに以下追加

ALLOWED_HOSTS = ['192.168.33.10']

webサーバーを再起動して再度アクセスすると正常に表示された

次にdatabase設定をする

sqliteを使うならあまりやることはないみたいだが今回はmysqlを使う

こちらの記事を参考にした

[root@localhost ~]# pip install pymysql

Collecting pymysql

 Downloading https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)

   100% |████████████████████████████████| 51kB 1.1MB/s

Installing collected packages: pymysql

Successfully installed pymysql-0.9.3

[root@localhost ~]# pip freeze -l

Django==2.1.4

PyMySQL==0.9.3

pytz==2018.7

selenium==3.141.0

urllib3==1.24.1

manage.pyに以下追加

import pymysql
pymysql.install_as_MySQLdb()

mysite/setting.pyにmysqlの設定を追加していく

DATABASES = {
“””
‘default’: {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: os.path.join(BASE_DIR, ‘db.sqlite3’),
}
“””
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘scraping’,
‘USER’: ‘root’,
‘PASSWORD’: ‘root’,
‘HOST’: ‘127.0.0.1’,
‘PORT’: ‘5432’,
}
}

タイムゾーンも変えるのでこちらの記事参考

TIME_ZONE = ‘Asia/Tokyo’

マイグレーションを実行してみると以下のエラー

[root@localhost mysite]# python manage.py migrate

・・・・

・・・・

django.db.utils.OperationalError: (2003, “Can’t connect to MySQL server on ‘127.0.0.1’ ([Errno 111] Connection refused)”)

database設定を以下のようにしたら行けた

‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘scraping’,
‘USER’: ‘root’,
‘PASSWORD’: ‘root’,
‘HOST’: ”,
‘PORT’: ”,
}
hostとportの指定はいらなかったらしい
なんでだろ

[root@localhost mysite]# python 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 sessions.0001_initial… OK

できたか確認

mysql -u root
mysql> use scraping
Database changed
mysql> show tables;

あと管理画面を使うために管理ユーザーを作成しておく

その前にmigrateをしてauth_userテーブルという管理ユーザーのテーブルを作っておく

[root@localhost feed_commenter_new]# python 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 sessions.0001_initial… OK

$ python manage.py createsuperuser

[root@localhost feed_commenter_new]# python manage.py createsuperuser

Username (leave blank to use ‘root’):

Email address:

Password:

Password (again):

This password is too short. It must contain at least 8 characters.

This password is too common.

Bypass password validation and create user anyway? [y/N]: y

Superuser created successfully.

ユーザ名は未入力だとrootで入るみたい

アドレスも未記入で大丈夫みたい

passwordも短すぎると警告出るみたいだけど大丈夫

コメント

タイトルとURLをコピーしました