码迷,mamicode.com
首页 > 其他好文 > 详细

Django学习路15_创建一个订单信息,并查询2020年\9月的信息都有哪些

时间:2020-05-11 13:17:32      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:doctype   auto   color   ima   for   type   size   nbsp   models   

在 app5.models.py 中添加一个 Order 表

class Order(models.Model):
    o_num = models.CharField(max_length= 16 ,unique=True)
    # 创建一个订单号,设置为 唯一
    o_time = models.DateTimeField(auto_now_add=True)
    # 创建一个时间,当对象进行保存时即可生成订单时间

注:
auto_now_add 当进行 save() 保存时,就会自动进行设置时间

产生迁移 -> 进行迁移

技术图片 


 

 

插入数据 如下所示

技术图片


技术图片

修改后的数据

技术图片


在 urls 中添加获取订单的函数 getorders

urlpatterns = [
    url(rgetuser/,views.get_user),
    url(rgetusers/,views.get_users),
    url(rgetorders,views.getorders)
]
在 views.py 中添加获取 2020年 的函数

def getorders(request):
orders = Order.objects.filter(o_time__year= 2020)
context = {
‘orders‘:orders
}
return render(request,‘orders.html‘,context = context)
在 templates 的 orders.html 中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul>
    {% for order in orders %}
    <li>{{ order.o_num }}</li>
    {% endfor %}
</ul>
</body>
</html>

技术图片

获取 9 月的信息

项目根目录的 settings.py 中的 USE_TZ = False 提前设置好

def getorders(request):
    orders = Order.objects.filter(o_time__month = 9)
    context = {
        orders:orders
    }
    return render(request,orders.html,context = context)

技术图片


技术图片


2020-05-11

 

Django学习路15_创建一个订单信息,并查询2020年\9月的信息都有哪些

标签:doctype   auto   color   ima   for   type   size   nbsp   models   

原文地址:https://www.cnblogs.com/hany-postq473111315/p/12868272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!