侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

django的ORM操作 增加和查询

框架(架构)  /  管理员 发布于 7年前   242

ORM 对象关系映射

在数据库中,实现对数据的增删改查,使用的是SQ语句,

在django中,通过python代码,实现对数据库的增删改查,这就是ORM。

在python中,用类名 代表 django数据库的表名,

用对象 ,代表django数据库的一条记录,

ORM 就是封装了SQ语句,给对象进行增删改查,实现对数据库的操作,

在settings 文件中,默认了splite的数据库,自己可以修改

DATABASES = {  'default': {    'ENGINE': 'django.db.backends.sqlite3',    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),  }}<br data-filtered="filtered"><br data-filtered="filtered">BASE_DIR 是代表当前的文件夹 settings,

django对数据库的迁移,只需要修改配置即可,

===

在model文件中,设计表,,

from django.db import models# Create your models here.#继承来自models ,class Book(models.Model):  name = models.CharField(max_length=32)  price = models.IntegerField()  Date = models.DateField()  auth = models.CharField(max_length=32)  publish = models.CharField(max_length=32)

命令台执行命令。

python manage.py makemigrations python manage.oy migrate 

生成数据库表

在template ,写一个主页index.html文件,用于显示添加后书籍

{% load staticfiles %} #  ------引用静态文件<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Title</title>  <link rel="stylesheet" href="https:/article/{% static '/bootstrap-3.3.7/dist/css/bootstrap.css/' %}" rel="external nofollow" >  # ---导入bootstrap 文件  <style>    .container{      margin-top: 50px;    }  </style></head><body><div class="container">  <div class="row">    <div class="col-md-6 col-md-offset-2">    <a href="https:addbook/" rel="external nofollow" ><button class="btn btn-primary">添加书籍</button></a>    <table class="table table-striped">      <tr>        <th>ID</th>        <th>书名</th>        <th>价格</th>        <th>出版日期</th>        <th>作者</th>        <th>出版社</th>      </tr>{#      <tr>#}{#        <td>1</td>#}{#        <td>水浒城</td>#}{#        <td>110</td>#}{#        <td>2011.1.1</td>#}{##}{#        <td>egon</td>#}{#        <td>人民出版社</td>#}{#      </tr>#}{#       <tr>#}{#        <td>2</td>#}{#        <td>水浒城</td>#}{#        <td>110</td>#}{#        <td>2011.1.1</td>#}{##}{#        <td>egon</td>#}{#        <td>人民出版社</td>#}{#      </tr>#}       {% for book in book_list %}  #--从数据库取到的数据是一个QuerySet集合,进行for循环,遍历出每个对象,      <tr>          <td>{{ book.id }}</td> #---把每个对象的属性的值渲染出来,          <td>{{ book.name }}</td>          <td>{{ book.price }}</td>          <td>{{ book.Date }}</td>          <td>{{ book.auth }}</td>          <td>{{ book.publish }}</td>      </tr>      {% endfor %}    </table>  </div>  </div></div></body><script></script></html>

写一个addbook.html 文件,用于添加书籍,然后跳转到index页面

{% load staticfiles %}<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Title</title>  <link rel="stylesheet" href="https:/article/{% static '/bootstrap-3.3.7/dist/css/bootstrap.css/' %} " rel="external nofollow" >  <style>    .container{      margin-top: 50px;    }  </style></head><body><div class="container">  <div class="row">    <div class="col-md-6 col-md-offset-2">{#      {{ csrf-token }}#}      <form class=addbook" action="/addbook/" method="post">{#      当点击添加按钮时,是执行视图函数,在数据库中添加一条记录,然后再把这个记录添加到index页面#}      <div class="form-group">        <label for="bookname">书名:</label>        <input type="text" class="form-control" id="bookname" name="bookname">  #---input 中的name属性,用于获取输入的值,      </div>      <div class="form-group">        <label for="price">价格:</label>        <input type="text" class="form-control" id="price" name="price"> #---input 中的name属性,用于获取输入的值,      </div>      <div class="form-group">        <label for="Date">日期:</label>        <input type="text" class="form-control" id="Date" name="Date">  #---input 中的name属性,用于获取输入的值,      </div>      <div class="form-group">        <label for="auth">作者:</label>        <input type="text" class="form-control" id="auth" name="auth">  #---input 中的name属性,用于获取输入的值,      </div>      <div class="form-group">        <label for="publish">出版社:</label>        <input type="text" class="form-control" id="publish" name="publish">  #---input 中的name属性,用于获取输入的值,      </div>      <input class="btn btn-info" type="submit" value='添加'>      </form>    </div>  </div></div></body></html>

在url文件中,匹配index页面和addbook页面的视图函数

from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [  url(r'^admin/', admin.site.urls),  url(r'^index/$',views.index),  url(r'^addbook/$',views.addbook),]

在views文件中,写逻辑代码。用户访问index页面,点击添加按钮,跳到addbook页面添加数据后,提交后,存到数据库,再从数据库拿到数据显示到index页面

步骤1,在index页面要显示数据库的信息,就要导入Book表,获取所有的数据,return render 进行渲染到页面展示

步骤2 ,addbook函数,从form表单中拿到添加的数据,从request里,以post的方法拿到,

存到数据库,用create方法,表名.objects .create(数据库的字段名= 表单中的name的属性名)

Book.objects.create(name = bookname,price = price, Date = Date, auth = auth , publish = publish)

左边的name 是models里的字段名,对应到form里取到的值,

from django.shortcuts import render,redirect# Create your views here.from app01.models import Bookdef index(request):  #把数据库的数据嵌入到页面进行显示  #查询api,所有书籍  book_list = Book.objects.all() #数据类型是QuerySet:[book1,book2...]  return render(request,'index.html',locals())def addbook(request):  #是form提交post的方法,  if request.method == 'POST':    #从form表单取数据    bookname = request.POST.get('bookname')    price = request.POST.get('price')    Date = request.POST.get('Date')    auth = request.POST.get('auth')    publish = request.POST.get('publish')  #把数据存到数据库,先把models 中的表引导到views文件  #方法1,左边的name 是models里的字段名,对应到form里取到的值,    Book.objects.create(name = bookname,price = price, Date = Date, auth = auth , publish = publish)  #存到数据库后要放到index页面,但现在是addbook页面,所以要跳转    return redirect('/index/')  return render(request,'addbook.html')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    解决django服务器重启端口被占用的问题
    下一条:
    Django在pycharm下修改默认启动端口的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客