上一页 1 ··· 14 15 16 17 18 19 20 21 22 ··· 24 下一页
摘要: HttpResponse 方法一: return HttpResponse('ok',headers={'xxx':'xxx'}) 方法二: obj = HttpResponse('ok') obj['yyy'] = 'yyy' # 像字典一样放入,最终会放在http的响应头中 return obj 阅读全文
posted @ 2023-11-30 15:19 wellplayed 阅读(41) 评论(0) 推荐(0)
摘要: 需要用到FunctionType和MethodType from types import FunctionType, MethodType 准备测试用代码 写一个函数: def add(): pass 写一个类: class Person: def run(self): pass @classme 阅读全文
posted @ 2023-11-30 15:07 wellplayed 阅读(40) 评论(0) 推荐(0)
摘要: 1.在应用(如:app01)下创建文件夹templatetags 再创建一个py文件,编写自定义标签(如:mytag) 2.编写自定义标签 from django import template register = template.Library() @register.filter(name= 阅读全文
posted @ 2023-11-29 21:33 wellplayed 阅读(66) 评论(0) 推荐(0)
摘要: 1.Template和Context的导入 from django.template import Template, Context 2.生成静态页面 ——在后端调用模板语法生成HTML页面,并保存到指定路径 2.1 我们想生成一个前端页面,代码如下 后端视图层传入的对象: user_data = 阅读全文
posted @ 2023-11-29 20:50 wellplayed 阅读(209) 评论(0) 推荐(0)
摘要: 绑定给类的方法,类来调用,对象可以调用吗? # 首先创建一个类,和绑定给类的方法index class MyClass: @classmethod def index(cls): print("hello index") 实例化对象: obj = MyClass() 使用对象调用绑定给类的方法: o 阅读全文
posted @ 2023-11-29 17:23 wellplayed 阅读(60) 评论(0) 推荐(0)
摘要: 1.路由配置 path('index/', 视图类名.as_view()) # as_view是类的绑定方法 2.执行流程(分析) path('index/', index), >请求来了,路由匹配成功会执行 index(request,) path('index/', UserView.as_vi 阅读全文
posted @ 2023-11-29 15:20 wellplayed 阅读(52) 评论(0) 推荐(0)
摘要: 1.视图层返回JsonResponse return JsonResponse({'name':'kevin','age':19}) 2.触发 JsonResponse 的__init__方法 将{'name':'kevin','age':19}传给data 3.源码分析 def __init__( 阅读全文
posted @ 2023-11-29 15:09 wellplayed 阅读(80) 评论(0) 推荐(0)
摘要: 我们可以使用使用GenericAPIView+序列化类+Response写接口 了解GenericAPIView 类属性: queryset:要序列化的所有数据 serializer_class:序列化类 lookup_field = 'pk' :查询单条时的key值,默认为pk,可以修改名称如'i 阅读全文
posted @ 2023-11-24 09:53 wellplayed 阅读(115) 评论(0) 推荐(0)
摘要: 外键中on_delete的方法: CASCADE:级联删除,只要删除publish,跟publish关联的book,全都被删除 SET_DEFAULT:只要删除publish,跟publish关联的book,的publish字段会变成默认值,一定要配合default使用 SET_NULL:只要删除p 阅读全文
posted @ 2023-11-24 09:06 wellplayed 阅读(127) 评论(0) 推荐(0)
摘要: 状态码 为了方便设置状态码,REST framewrok在rest_framework.status模块中提供了常用状态码常量。 导入: from rest_framework import status 提示: 默认响应状态码200 1)信息告知 - 1xx HTTP_100_CONTINUE(继 阅读全文
posted @ 2023-11-23 21:17 wellplayed 阅读(82) 评论(0) 推荐(0)
上一页 1 ··· 14 15 16 17 18 19 20 21 22 ··· 24 下一页