1.確保已經安裝好python和Django
2.創建django項目
3.打開setting.py配置文件
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blog', //數據庫名稱
'USER': 'root', //用戶名
'PASSWORD': '123456', //密碼
'HOST': 'localhost', //地址
'PORT': '3306', //端口
}
}
4.查看是否安裝有pymysql,若未安裝,使用pip install pymysql
5.進入django項目根目錄,運行程序:
python manage.py runserver
程序會顯示如下錯誤:
_mysql.c(29): fatal error C1083: 無法打開包括文件: “mysql.h”: No such file or directory
error: command 'D:\\vs2015\\VC\\BIN\\amd64\\cl.exe' failed with exit status 2
這是驅動的問題,往下面的鏈接中下載MySQL-python-1.2.3.win-amd64-py2.7.exe
鏈接地址:http://www.codegood.com/archives/129 (注意下載過程中要根據系統版本進行下載安裝)
6.執行第五步,若顯示如下,證明配置成功
D:\pythonPro\new_file\djangoBlog>python manage.py runserver
Performing system checks...
2017-12-20 10:06:30,566 [utils:execute] [DEBUG]- (0.000) SELECT @@SQL_AUTO_IS_NULL; args=None
2017-12-20 10:06:30,566 [utils:execute] [DEBUG]- (0.000) SELECT VERSION(); args=None
System check identified no issues (0 silenced).
2017-12-20 10:06:30,657 [utils:execute] [DEBUG]- (0.000) SHOW FULL TABLES; args=None
2017-12-20 10:06:30,667 [utils:execute] [DEBUG]- (0.000) SELECT `django_migrations`.`app`, `django_migrations`.`name` FROM `django_migrations`; args=()
December 20, 2017 - 10:06:30
Django version 1.11.5, using settings 'djangoBlog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
2017-12-20 10:06:36,788 [basehttp:log_message] [INFO]- "GET / HTTP/1.1" 200 445
2017-12-20 10:06:36,957 [basehttp:log_message] [INFO]- "GET /get_count HTTP/1.1" 301 0
2017-12-20 10:06:36,957 [basehttp:log_message] [INFO]- "GET /get_articles HTTP/1.1" 301 0
2017-12-20 10:06:36,957 [basehttp:log_message] [INFO]- "GET /get_rightbox_content HTTP/1.1" 301 0