备注:一定要将setting.py 下的debug 设置成True,千万不要写成False
1.在BBT目录下创建博客应用
D:\python3\Scripts\BBT>python3 manage.py startapp blog # 创建blog应用
2.打开settings.py 配置文件,添加blog应用。
3.配置mysql数据库
3.1mysql 下载地址:
下载 MySQL:http://dev.mysql.com/downloads/mysql/
安装mysql 教程,访问:http://www.jb51.net/article/92158.htm
安装成功后,创建数据库BBT
mysql> create database BBT;
Query OK, 1 row affected (0.01 sec)
3.2安装 PyMySQL
C:\Users\fnngj>python3 -m pip install PyMySQL
3.3Django 配置 MySQL
需要在.../BBT/settings.py 文件中修改数据库相关配置。
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
'ENGINE': 'django.db.backends.mysql',
'NAME':'BBT',
'USER':'root',
'PASSWORD':'123456',
'HOST':'127.0.0.1',
'PORT':'3306'
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
配置信息从上到下依次是驱动(ENGINE),主机地址(HOST),端口号(PORT),数据库(NAME), 登录用户名(USER),登录密码(PASSWORD)。
3.4修改默认后台为中文,修改.../BBT/settings.py
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE='zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
# TIME_ZONE = 'UTC'
3.5在.../guest/__init__.py 目录下添加:
在cmd窗口,进入BBT目录,执行以下命令,同步数据
D:\python3\Scripts\BBT>python3 manage.py migrate
结果如下:
D:\python3\Scripts\BBT>python3 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 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
D:\python3\Scripts\BBT>
3.6创建admin后台的超级管理员账号(admin/admin123456)
C:\Users\Administrator>D:
D:\>cd python3
D:\python3>cd Scripts
D:\python3\Scripts>cd BBT
D:\python3\Scripts\BBT>python3 manage.py createsuperuser
Username (leave blank to use 'administrator'): admin
Email address: 1421914467@qq.com
Password:
Password (again):
Superuser created successfully.
D:\python3\Scripts\BBT>
3.7启动程序,登陆后台
http://127.0.0.1:8000/admin