@rayiooo
2018-05-26T03:50:52.000000Z
字数 3588
阅读 1179
Readme
这是一个Django博客。
| Author | 爱吃大板 |
|---|---|
| rayiooo@foxmail.com |
在PyCharm的File/New Project中新建一个Django项目。
在mysite/settings.py中修改数据库类型为MySQL,如下:
import pymysqlpymysql.install_as_MySQLdb()DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': 'blogdb','USER': 'root','PASSWORD': '123456','HOST': '127.0.0.1','PORT': '3306',}}
通过phpStudy的MySQL管理器进入数据库,新建一个数据库blogdb,并保持数据库登入账号密码与上面的值相同。
Django默认帮我们做很多事情,比如User、Session 这些都需要创建表来存储数据,Django已经把这些模块帮我准备好了,我们只需要执行数据库同步,把相关表生成出来即可:↻
mysite> python manage.py migrateOperations to perform:Apply all migrations: admin, auth, contenttypes, sessionsRunning migrations:Applying contenttypes.0001_initial... OKApplying auth.0001_initial... OKApplying admin.0001_initial... OKApplying admin.0002_logentry_remove_auto_add... OKApplying contenttypes.0002_remove_content_type_name... OKApplying auth.0002_alter_permission_name_max_length... OKApplying auth.0003_alter_user_email_max_length... OKApplying auth.0004_alter_user_username_opts... OKApplying auth.0005_alter_user_last_login_null... OKApplying auth.0006_require_contenttypes_0002... OKApplying auth.0007_alter_validators_add_error_messages... OKApplying auth.0008_alter_user_username_max_length... OKApplying auth.0009_alter_user_last_name_max_length... OKApplying sessions.0001_initial... OK
如果在这个过程中出现任何错误,参照 django数据库错误相关问题。
要想登录admin后台,必须要有帐号,接下来创建超级管理员帐号。↻
mysite> python manage.py createsuperuserUsername (leave blank to use 'fnngj'): admin # 管理员帐号Email address: admin@mail.com # emailPassword: # 密码Password (again): # 重复密码Superuser created successfully.
在PyCharm中直接运行程序,服务器即可启动。当然也可以通过python manage.py runserver命令行指令执行。
访问 127.0.0.1:8000 以及 127.0.0.1:8000/admin 即可在本地查看主界面和admin界面。
特别参考:windows 下 apache 部署 django python3.6
特别强调注意的是:apache 的位制和python的位制必须一致(即apache 32位的就只能安装python 32位的,64位也一样)网上很多教程没强调这一点,导致后续的很多工作白费,还找不到问题所在!
因此如果使用phpStudy的apache 32位,就必须使用python 32位。
通过pip install mod_wsgi安装mod_wsgi。
如果无法直接安装,就到https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi下载whl文件,放到任意目录下,并在该目录下启动cmd,输入pip install 文件名.whl安装模块。
比如Apache2.4、python3.6/32位就选择下载mod_wsgi‑4.6.4+ap24vc14‑cp36‑cp36m‑win32.whl。
在命令行中输入以下指令:
mod_wsgi-express module-config
可以看到以下wsgi模块位置的输出结果,将它们复制下来。(在cmd中选中、右击即可复制粘贴。)
LoadFile "c:/program files (x86)/python36/python36.dll"
LoadModule wsgi_module "c:/program files (x86)/python36/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win32.pyd"
WSGIPythonHome "c:/program files (x86)/python36"
在phpStudy软件中点击其他选项菜单/打开配置文件/httpd-conf,把这三行内容复制到到LoadModule最后的部分(应该也可以复制到整个文件的最后部分)。
同样,在apache的http.conf末尾添加下面的内容。目录不同,请对照自己的项目做相应更改。
#指定website的wsgi.py配置文件路径WSGIScriptAlias / C:/Users/Administrator/Desktop/WebServer/_180524_BlogRayiooo/mysite/wsgi.py#指定项目路径WSGIPythonPath C:/Users/Administrator/Desktop/WebServer/_180524_BlogRayiooo<Directory C:/Users/Administrator/Desktop/WebServer/_180524_BlogRayiooo/mysite><Files wsgi.py>Require all grantedsetHandler wsgi-script</Files></Directory>
在项目的wsgi.py文件中import os后添加如下代码。
import sysBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))sys.path.append(BASE_DIR)
在phpStudy中重启apache,从本地(127.0.0.1)和外网(服务器地址)分别访问,查看是否配置成功。
如果出现HTTP 500 - Internal server error错误,在apache/logs/error.log中查看错误原因。
如果出现DisallowedHost at /错误,就在项目的settings.py文件下做出更改:
ALLOWED_HOSTS = ['*']