摘要: sqlite3: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', #'NAME': BASE_DIR / 'db.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlit 阅读全文
posted @ 2022-04-05 20:10 咖喱给给啊 阅读(33) 评论(0) 推荐(0)
摘要: CASCADE:这就是默认的选项,级联删除,你无需显性指定它。 PROTECT: 保护模式,如果采用该选项,删除的时候,会抛出ProtectedError错误。 SET_NULL: 置空模式,删除的时候,外键字段被设置为空,前提就是blank=True, null=True,定义该字段的时候,允许为 阅读全文
posted @ 2022-04-05 19:56 咖喱给给啊 阅读(435) 评论(0) 推荐(0)
摘要: """ 由于django自带的sqlite数据库对日期不敏感,所以我们换成MySQL """ from django.db import models # Create your models here. """ 先写普通字段 之后再写外键字段 """ from django.contrib.aut 阅读全文
posted @ 2022-04-05 19:46 咖喱给给啊 阅读(29) 评论(0) 推荐(0)
摘要: from django.db import models from django.contrib.auth.models import User,AbstractUser # Create your models here. # 第一种:一对一关系 不推荐 # class UserDetail(mo 阅读全文
posted @ 2022-04-05 19:41 咖喱给给啊 阅读(94) 评论(0) 推荐(0)
摘要: from django.shortcuts import render,redirect,HttpResponse from django.contrib import auth # Create your views here. from django.contrib.auth.decorator 阅读全文
posted @ 2022-04-05 19:13 咖喱给给啊 阅读(410) 评论(0) 推荐(0)
摘要: """ 其实我们在创建好一个django项目之后直接执行数据库迁移命令会自动生成很多表 django_session auth_user django在启动之后就可以直接访问admin路由,需要输入用户名和密码,数据参考的就是auth_user表,并且还必须是管理员用户才能进入 创建超级用户(管理员 阅读全文
posted @ 2022-04-05 18:37 咖喱给给啊 阅读(30) 评论(0) 推荐(0)
摘要: from django.views.decorators.csrf import csrf_protect,csrf_exempt from django.utils.decorators import method_decorator """ csrf_protect 需要校验 针对csrf_pr 阅读全文
posted @ 2022-04-05 16:50 咖喱给给啊 阅读(13) 评论(0) 推荐(0)
摘要: 在静态文件夹中建一个js文件 内容: function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie !== '') { var cookies = document.cookie.s 阅读全文
posted @ 2022-04-05 16:39 咖喱给给啊 阅读(28) 评论(0) 推荐(0)
摘要: 一些细节: 使用process_template_response的时候 需要在视图中这样写,这个东西你自己是想不到的,这个是别人规定好的,不是逻辑问题,是语法问题 def index(request): print('我是视图函数index') obj = HttpResponse('index' 阅读全文
posted @ 2022-04-05 16:18 咖喱给给啊 阅读(22) 评论(0) 推荐(0)
摘要: from django.views import View from django.utils.decorators import method_decorator """ CBV中django不建议你直接给类的方法加装饰器 无论该装饰器能都正常给你 都不建议直接加 """ # @method_de 阅读全文
posted @ 2022-04-05 05:36 咖喱给给啊 阅读(18) 评论(0) 推荐(0)
摘要: """ session数据是保存在服务端的(存?),给客户端返回的是一个随机字符串 sessionid:随机字符串 1.在默认情况下操作session的时候需要django默认的一张django_session表 数据库迁移命令 django会自己创建很多表 django_session就是其中的一 阅读全文
posted @ 2022-04-05 05:35 咖喱给给啊 阅读(84) 评论(0) 推荐(0)
摘要: """ 发展史 1.网站都没有保存用户功能的需求 所有用户访问返回的结果都是一样的 eg:新闻、博客、文章... 2.出现了一些需要保存用户信息的网站 eg:淘宝、支付宝、京东... 以登陆功能为例:如果不保存用户登陆状态 也就意味着用户每次访问网站都需要重复的输入用户名和密码(你觉得这样的网站你还 阅读全文
posted @ 2022-04-05 05:34 咖喱给给啊 阅读(19) 评论(0) 推荐(0)
摘要: # 虽然cookie是服务端告诉客户端浏览器需要保存内容 # 但是客户端浏览器可以选择拒绝保存 如果禁止了 那么 只要是需要记录用户状态的网站登陆功能都无法使用了 # 视图函数的返回值 return HttpResponse() return render() return redirect() o 阅读全文
posted @ 2022-04-05 05:34 咖喱给给啊 阅读(30) 评论(0) 推荐(0)
摘要: ```python label 字段名 error_messages 自定义报错信息 initial 默认值 required 控制字段是否必填 """ 1.字段没有样式 2.针对不同类型的input如何修改 text password date radio checkbox ... """ wid 阅读全文
posted @ 2022-04-05 05:05 咖喱给给啊 阅读(60) 评论(0) 推荐(0)
摘要: """ 在特定的节点自动触发完成响应操作 钩子函数在forms组件中就类似于第二道关卡,能够让我们自定义校验规则 在forms组件中有两类钩子 1.局部钩子 当你需要给单个字段增加校验规则的时候可以使用 2.全局钩子 当你需要给多个字段增加校验规则的时候可以使用 """ # 实际案例 # 1.校验用 阅读全文
posted @ 2022-04-05 05:04 咖喱给给啊 阅读(94) 评论(0) 推荐(0)
摘要: """ 浏览器会自动帮你校验数据 但是前端的校验弱不禁风 如何让浏览器不做校验 <form action="" method="post" novalidate> """ def index(request): # 1 先产生一个空对象 form_obj = MyForm() if request. 阅读全文
posted @ 2022-04-05 05:03 咖喱给给啊 阅读(50) 评论(0) 推荐(0)
摘要: """ forms组件只会自动帮你渲染获取用户输入的标签(input select radio checkbox) 不能帮你渲染提交按钮 """ def index(request): # 1 先产生一个空对象 form_obj = MyForm() # 2 直接将该空对象传递给html页面 ret 阅读全文
posted @ 2022-04-05 05:02 咖喱给给啊 阅读(41) 评论(0) 推荐(0)
摘要: from django import forms class MyForm(forms.Form): # username字符串类型最小3位最大8位 username = forms.CharField(min_length=3,max_length=8) # password字符串类型最小3位最大 阅读全文
posted @ 2022-04-05 05:01 咖喱给给啊 阅读(62) 评论(0) 推荐(0)
摘要: """ 总数据100 每页展示10 需要10 总数据101 每页展示10 需要11 总数据99 每页展示10 需要10 如何通过代码动态的计算出到底需要多少页? 在制作页码个数的时候 一般情况下都是奇数个 符合中国人对称美的标准 """ # 分页 book_list = models.Book.ob 阅读全文
posted @ 2022-04-05 04:48 咖喱给给啊 阅读(91) 评论(0) 推荐(0)
摘要: # 先给Book插入一万条数据 # for i in range(10000): # models.Book.objects.create(title='第%s本书'%i) # # 再将所有的数据查询并展示到前端页面 book_queryset = models.Book.objects.all() 阅读全文
posted @ 2022-04-05 04:47 咖喱给给啊 阅读(90) 评论(0) 推荐(0)
摘要: """ 自己要学会如何拷贝 学会基于别人的基础之上做修改 研究各个参数表示的意思 然后找葫芦画瓢 """ <script> $('.del').on('click',function () { // 先将当前标签对象存储起来 let currentBtn = $(this); // 二次确认弹框 s 阅读全文
posted @ 2022-04-05 04:43 咖喱给给啊 阅读(23) 评论(0) 推荐(0)
摘要: ### ajax发送文件 ```python """ ajax发送文件需要借助于js内置对象FormData """ <script> // 点击按钮朝后端发送普通键值对和文件数据 $('#d4').on('click',function () { // 1 需要先利用FormData内置对象 le 阅读全文
posted @ 2022-04-05 04:23 咖喱给给啊 阅读(834) 评论(0) 推荐(0)
摘要: ### ajax发送json格式数据 ```python """ 前后端传输数据的时候一定要确保编码格式跟数据真正的格式是一致的 不要骗人家!!! {"username":"jason","age":25} 在request.POST里面肯定找不到 django针对json格式的数据 不会做任何的处 阅读全文
posted @ 2022-04-05 04:23 咖喱给给啊 阅读(109) 评论(0) 推荐(0)
摘要: # 我们主要研究post请求数据的编码格式 """ get请求数据就是直接放在url后面的 url?username=jason&password=123 """ # 可以朝后端发送post请求的方式 """ 1.form表单 2.ajax请求 """ """ 前后端传输数据的编码格式 urlenc 阅读全文
posted @ 2022-04-05 04:22 咖喱给给啊 阅读(114) 评论(0) 推荐(0)
摘要: """ 异步提交 局部刷新 例子:github注册 动态获取用户名实时的跟后端确认并实时展示的前端(局部刷新) 朝发送请求的方式 1.浏览器地址栏直接输入url回车 GET请求 2.a标签href属性 GET请求 3.form表单 GET请求/POST请求 4.ajax GET请求/POST请求 A 阅读全文
posted @ 2022-04-05 03:31 咖喱给给啊 阅读(20) 评论(0) 推荐(0)
摘要: # 全自动:利用orm自动帮我们创建第三张关系表 class Book(models.Model): name = models.CharField(max_length=32) authors = models.ManyToManyField(to='Author') class Author(m 阅读全文
posted @ 2022-04-05 03:30 咖喱给给啊 阅读(24) 评论(0) 推荐(0)
摘要: # MTV:Django号称是MTV模型 M:models T:templates V:views # MVC:其实django本质也是MVC M:models V:views C:controller # vue框架:MVVM模型 阅读全文
posted @ 2022-04-05 03:29 咖喱给给啊 阅读(19) 评论(0) 推荐(0)
摘要: class School(models.Model): """ 校区表 如: 北京沙河校区 上海校区 """ title = models.CharField(verbose_name='校区名称', max_length=32) def __str__(self): return self.tit 阅读全文
posted @ 2022-04-05 03:27 咖喱给给啊 阅读(32) 评论(0) 推荐(0)
摘要: """ 用户表 性别 学历 工作经验 是否结婚 是否生子 客户来源 ... 针对某个可以列举完全的可能性字段,我们应该如何存储 只要某个字段的可能性是可以列举完全的,那么一般情况下都会采用choices参数 """ class User(models.Model): username = model 阅读全文
posted @ 2022-04-05 03:26 咖喱给给啊 阅读(28) 评论(0) 推荐(0)
摘要: ```python from django.shortcuts import render,redirect,HttpResponse from app01 import models # Create your views here. def home(request): return rende 阅读全文
posted @ 2022-04-05 03:23 咖喱给给啊 阅读(80) 评论(0) 推荐(0)
摘要: ```python only与defer select_related与prefetch_related """ orm语句的特点: 惰性查询 如果你仅仅只是书写了orm语句 在后面根本没有用到该语句所查询出来的参数 那么orm会自动识别 直接不执行 """ # only与defer # res = 阅读全文
posted @ 2022-04-05 03:15 咖喱给给啊 阅读(31) 评论(0) 推荐(0)
摘要: AutoField 主键字段 primary_key=True CharField varchar verbose_name 字段的注释 max_length 长度 IntegerField int BigIntegerField bigint DecimalField max_digits=8 d 阅读全文
posted @ 2022-04-05 02:35 咖喱给给啊 阅读(35) 评论(0) 推荐(0)
摘要: """ 事务 ACID 原子性 不可分割的最小单位 一致性 跟原子性是相辅相成 隔离性 事务之间互相不干扰 持久性 事务一旦确认永久生效 事务的回滚 rollback 事务的确认 commit """ # 目前你只需要掌握Django中如何简单的开启事务 # 事务 from django.db im 阅读全文
posted @ 2022-04-05 02:34 咖喱给给啊 阅读(79) 评论(0) 推荐(0)
摘要: # F查询 # 1.查询卖出数大于库存数的书籍 # F查询 """ 能够帮助你直接获取到表中某个字段对应的数据 """ from django.db.models import F # res = models.Book.objects.filter(maichu__gt=F('kucun')) # 阅读全文
posted @ 2022-04-05 02:20 咖喱给给啊 阅读(103) 评论(0) 推荐(0)
摘要: #统计每个作者出了多少本书 # res = models.Book.objects.annotate(author_num=Count('authors__name')).values('title','author_num') # print(res) #其实直接按照书的名字来统计就可以了,但是必 阅读全文
posted @ 2022-04-05 01:15 咖喱给给啊 阅读(29) 评论(0) 推荐(0)