jerryxjr1220 发表于 2020-5-19 20:19:17

【原创教程】建站实录系列之Django3+Xadmin2+CKeditor打造个人博客(5)

4. 配置urls.py及其他设置并初步运行网站

其实在配置urls.py之前,可以先利用manage.py管理工具对数据库进行初始化。

在PyCharm中运行manage.py更方便,如下图
http://bestech.ren/media/images/4_20200504073253_207.jpg

然后在对话框输入数据库迁移命令,makemigrations #创建迁移;migrate #执行迁移

manage.py@MyBlog > makemigrations
manage.py@MyBlog > migrate
Tracking file by folder pattern:migrations
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 auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK
Process finished with exit code 0

可以看到django会默认生成一系列默认的数据库表,将来所有的数据都会存放在数据库中。

按个人习惯,还可以对刚刚生成的数据库进行改名,在PyCharm中改名也非常方便,并且PyCharm会对引用的地方进行跟踪修改
http://bestech.ren/media/images/5_20200504074239_620.jpg
下面再来看一下urls.py的配置

from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include

urlpatterns = [
    path('admin/', admin.site.urls),
]

django已经默认帮我们做了一个admin路径的配置,用于登录django默认的后台管理系统。

要登录后台管理系统的话,我们还需要创建一个管理员账号,用manage.py来创建,创建命令createsuperuser

manage.py@MyBlog > createsuperuser
Tracking file by folder pattern:migrations
用户名 (leave blank to use '******'):
电子邮件地址:******
Warning: Password input may be echoed.
Password:******
Warning: Password input may be echoed.
Password (again):******
密码跟 电子邮件地址 太相似了。
密码长度太短。密码必须包含至少 8 个字符。
Bypass password validation and create user anyway? :y
Superuser created successfully.

Process finished with exit code 0

按照提示进行一系列操作,当出现

Superuser created successfully.

就表示创建成功了,然后运行网站,我们就可以用管理员账号登录后台管理系统了。

运行网站,可以在manage.py中用runserver命令,也可以直接让PyCharm启动,点击右上角绿色三角。

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
May 04, 2020 - 07:54:24
Django version 3.0.4, using settings 'MyBlog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

当出现如上述提示,表示系统已经运行了,可以直接登录http://127.0.0.1:8000

可以看到Django的默认欢迎页面,也就意味着登录成功了。

登录http://127.0.0.1:8000/admin

用管理员账号登录,就可以看到默认的后台管理系统了,可以对用户及管理组和权限进行相关设置。

jerryxjr1220 发表于 2020-5-20 08:17:47

发教程都要审核了
页: [1]
查看完整版本: 【原创教程】建站实录系列之Django3+Xadmin2+CKeditor打造个人博客(5)