文章目录
django routing approach&RESTful api development
references
- 路由步骤和路由编写:URL dispatcher | Django documentation | Django (djangoproject.com)
记号声明
路由的组成
url
view
- a Python function
- a class-based view
arguments passed to view(视图获得的参数)
注册(激活app)
app路由方式和路由配置
先找到配置的总路由
在django项目中的管理目录中
与项目根目录同名的子文件夹中保存该项目的配置文件(譬如总路由等)
配置全局空路由
path('',include('index.urls')),
后续路由(路由url到视图view)
-
在总路由中匹配到具体的django ap(urlpattern不必然是某个app(譬如为空:
path('',include('index.urls')),
),(但是include()需要传入app/urls); -
view.py中可以包编写多个view页面,不同的url中的路径由不同的view来处理
-
在对应的app中有分支路由(在app 的urls.py中配置对应到具体的django view)
URL pattern
- A URL pattern is the general form of a URL - for example:
/newsarchive/<year>/<month>/
.
url to view& URLconfs
- To get from a URL to a view, Django uses what are known as ‘URLconfs’. A URLconf maps URL patterns to views.
admin后台管理
- 通过创建超级用户createsuperuser,等操作,
- 启动admin管理服务需要一个未被占用的端口
- 类似于总路由
- 我们创建的django app 需要在各自app目录中的admin.py文件中注册才可以在后台管理中看到相关应用
数据模型与数据库操作
references
- Making queries | Django documentation | Django (djangoproject.com)
导入操作
- 一般我们导入的是一个类(类名通常大写)
- 而类所在的文件(或者找到该类的路径)各个单词首字母一般小写
模型类管理者接口object
- Model(或者subClassOfModel).objects对象作为该模型的管理者:(
objects: BaseManager[Any]
) - 通过该管理对象提供对模型(所映射的表)进行CRUD操作
数据库迁移(模型结构和数据库同步)
- Change your models (in
models.py
). - Run
python manage.py makemigrations
to create migrations for those changes - Run
python manage.py migrate
to apply those changes to the database.
sql查询和下划线参数解析
- Field lookupslink¶
双下划线
Field lookups are how you specify the meat of an SQL WHERE
clause. They’re specified as keyword arguments to the QuerySet
methods filter()
, exclude()
and get()
.
Basic lookups keyword arguments take the form field__lookuptype=value
. (That’s a double-underscore). For example:
>>> Entry.objects.filter(pub_date__lte='2006-01-01')#__lte:<=(小于等于)
translates (roughly) into the following SQL:
SELECT * FROM blog_entry WHERE pub_date <= '2006-01-01';
How this is possible
Python has the ability to define functions that accept arbitrary name-value arguments whose names and values are evaluated at runtime. For more information, see Keyword Arguments in the official Python tutorial.
单下划线
-
The field specified in a lookup has to be the name of a model field.
-
There’s one exception though, in case of a
ForeignKey
you can specify the field name suffixed with_id
. -
In this case, the value parameter is expected to contain the raw value of the foreign model’s primary key. For example:
>>> Entry.objects.filter(blog_id=4)
DRF框架
- Quickstart - Django REST framework中文站点 (q1mi.github.io)
- Home - Django REST framework (django-rest-framework.org)
序列化器
-
序列化器是DRF的核心
- 我在编写ViewSet的时候要引用我们为app专门编写的序列化器类
-
另一个的路由的生成
- 这使得我们可以少写路由配置,由DRF router来生成RESTful 路由
参考文档目录
- URL调度器
- 概况
- Django 如何处理一个请求
- 例如
- 路径转换器
- 注册自定义的路径转换器
- 使用正则表达式
- 使用未命名的正则表达式组
- 嵌套参数
- URLconf 在什么上查找
- 指定视图参数的默认值
- 性能
urlpatterns
变量的语法- 错误处理
- 包含其它的URLconfs
- 捕获的参数
- 传递额外选项给视图函数
- 传递额外选项给
include()
- 传递额外选项给
- URL 的反向解析
- 示例
- 命名 URL 模式
- URL 命名空间
- 介绍
- 反向命名空间 URLs
- 例如
- URL 命名空间和包含的 URLconfs
mirrors_cheat/cheatsheets (gitee.com)
How to install man pages on Ubuntu Linux - nixCraft (cyberciti.biz)
Linux man Command | Learn How to Use Man Command in Linux? (educba.com)