在成功安裝MySQL-python-1.2.5后,開始配置django的mysql連接配置。 打開django項目的二級目錄/Hello/Hello/setting.py文件。 默認情況下Django數據為sqlite:
# Database# https://docs.djangoPRoject.com/en/dev/ref/settings/#databasesDATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), }}現在我們將它修改為mysql數據庫
# Database# https://docs.djangoproject.com/en/dev/ref/settings/#databasesDATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mysite', #數據庫名稱 'USER': 'root', #數據庫的用戶名 'PASSWord': '123', #數據庫對應用戶的密碼 'HOST': '127.0.0.1', #數據庫主機 'PORT': '3306', #數據庫默認端口號 }}執行數據庫同步腳本:
python mange.py syncdb上面腳本可能在Django高版本執行報錯,1.7及以上可以使用下邊:
python manage.py makemigrationspython manage.py migrate執行結果
im@58user:~/PythonProjects/Hello$ python manage.py migrateSystem check identified some issues:Operations to perform: Apply all migrations: admin, auth, contenttypes, sessionsRunning 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新聞熱點
疑難解答