Django之ORM使用以及模板语言

一、ORM版增删改查

  1、ORM的语句

    1、类名.objects.all()          --> 返回一个列表

    2、类名.objects.filter()       --> 返回一个列表

    3、类名.objects.get()         --> 返回一个对象

    4、类名.objects.create(name='')    --> 创建一个对象,返回的就是刚创建的对象

    5、类名.objects.filter(id='').delete()   --> 删除

    6、obj = 类名.objects.get(id='')   

      obj.name = '新值'                    --> 修改对象的属性

      obj.save()                                   --> 把修改后的值同步到数据库

  2、Django模板语言

    1、for循环

      {% for i in ret %}

        {{ i }}

        {{ forloop.counter }}            --> for 循环从1开始计数

        {{ forloop.counter0 }}       --> for 循环从0开始计数

      {% endfor %}

 1 from django.shortcuts import render,redirect
 2 from app01.models import Press
 3 
 4 # Create your views here.
 5 
 6 
 7 # 出版社列表函数
 8 def press_list(request):
 9     # 获取出版社数据库中的所有数据
10     ret = Press.objects.all()
11     # 用html展示出版社列表
12     return render(request,'press_list.html',{'ret':ret})
13 
14 
15 # 添加出版社函数
16 def add_press(request):
17     # 判断是不是POST请求方法(form表单中)
18     if request.method == 'POST':
19         # 获取要添加的名字
20         new_name = request.POST.get('name')
21         # 在数据库中创建新的出版社
22         Press.objects.create(name=new_name)
23         # 跳转到出版社列表页面
24         return redirect('/press_list/')
25     # 跳转到添加页面
26     return render(request,'add_press.html')
27 
28 
29 # 删除出版社函数
30 def delete_press(request):
31     # 获取要删除出版社的ID
32     delete_id = request.GET.get('id')
33     # 根据获取的ID来删除数据库中对应的出版社
34     Press.objects.get(id=delete_id).delete()
35     # 跳转到出版社列表
36     return redirect('/press_list/')
37 
38 
39 # 编辑出版社函数
40 def edit_press(request):
41     # 获取要编辑的ID
42     edit_id = request.GET.get('id')
43     # 通过ID来获取对象
44     press_obj = Press.objects.get(id=edit_id)
45     # 判断是不是POST请求(form表单中)
46     if request.method == 'POST':
47         # 获取修改以后的name
48         edit_name = request.POST.get('name')
49         # 重新给name赋值
50         press_obj.name = edit_name
51         # 把修改以后的name更新到数据库
52         press_obj.save()
53         # 跳转到出版社列表
54         return redirect('/press_list/')
55     # 跳转到编辑页面
56     return render(request,'edit_press.html',{'press_obj': press_obj})
57 
58 views.py中所写的内容
views.py中的内容

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=device-width, initial-scale=1">
 6     <title>出版社页面</title>
 7     <link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.css">
 8     <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.css">
 9     <link rel="stylesheet" href="/static/bootstrap-sweetalert-master/dist/sweetalert.css">
10     <script src="/static/bootstrap-sweetalert-master/dist/sweetalert.js"></script>
11     <script src="/static/jquery-3.3.1.min.js"></script>
12     <script src="/static/bootstrap-3.3.7/js/bootstrap.js"></script>
13     <style>
14         table {
15 
16             border-spacing: 2px;
17             border-collapse: unset;
18         }
19         body{
20             font-size: 16px;
21         }
22     </style>
23 </head>
24 <body>
25 <table border="1">
26     <thead>
27     <tr>
28         <th>序号</th>
29         <th>id</th>
30         <th>name</th>
31         <th>操作</th>
32     </tr>
33     </thead>
34     <tbody>
35     {% for foo in ret %}
36         <tr>
37             <td>{{ forloop.counter }}</td>
38             <td>{{ foo.id }}</td>
39             <td>{{ foo.name }}</td>
40             <td>
41                 <a href="/edit_press/?id={{ foo.id }}">编辑</a>
42                 <a href="/delete_press/?id={{ foo.id }}">删除</a>
43             </td>
44         </tr>
45     {% endfor %}
46     </tbody>
47 </table>
48 <button class="btn-success btn"><a href="/add_press/" style="text-decoration: none;color: blue;">添加出版社</a></button>
49 </body>
50 </html>
press_list.html文件

 

转载于:https://www.cnblogs.com/wjs521/p/9716059.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值