大纲
目录结构
学习新知识的建议原则
path函数
route 参数
view 参数
kwargs 参数
转换器 + kwargs 参数
name 参数
1、先运行 manage.py 文件 ,点击 Edit Configurations 进入
2、配置: 输入 runserver,点击ok
再次运行
3、配置数据库
配置好后运行,会报错,第一先安装 pymysql,第二修改mysql 名字
进入 __init__文件修改
修改后 运行出现以下命令 是提醒 需要迁移
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
4、迁移
(class15env) PS D:\muyi\Django\muyi> python .\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 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 auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK
5、创建后台管理账号
(class15env) PS D:\muyi\Django\muyi> python .\manage.py createsuperuser # 创建命令
Username (leave blank to use '37410'): admin # 账号
Email address: admin@qq.qq.com # 邮箱
Password: # 密码 qw123
Password (again): # 第二次输入密码 qw123
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
6、登录并进入后台管理
7、进入数据库查看创建的表
use class15db; 进入库
show tables; 查看所有表
8、查询表的所有字段
select * from 表名
目录结构
项目入口文件 :manage.py
项目目录
应用目录
创建应用目录
(class15env) PS D:\muyi\Django\muyi> django-admin startapp the_2
学习新知识的建议原则
在合适的时机做合适的事
不懂的暂时不过度深思
能做出效果为第一优先级
最小化知识学习粒度 —— 控制变量法
path 函数
path(route, view, kwargs=None, name=None, Pattern=None)
route 参数 —— 路径参数,路径名字入口
view 参数 —— 指明路径对应的视图,可传参
常用转换器:int: str: uuid: —— uuid 是:通用唯一标识符
kwargs 参数 —— 属于键值对
传多值
name 参数 —— 为 url 取一个别名,通过这个名字可以逆向找到url路径
总结 —— 案例
json 格式