djgo queryset和字典使用

工作   2023-07-23 22:26   727   3  

Dj go queryset和字典使用


一.queryset

urls


urlpatterns = {
# path('admin/', admin.site.urls),
   url(r'^login', views.login),
   url(r'^home', views.home),
   url(r'^hty', views.hty),
   url(r'^useradd', views.useradd),
   url(r'^ui', views.ui),
}

  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

2.views.py


def ui(request):
   ui = models.UseInfo.objects.all()
   print(ui)
   print(type(ui))
   return render(request, 'ui.html', {'ui': ui})

  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

3.models


class UseInfo(models.Model):
   username = models.CharField(max_length=32)
   passwd = models.CharField(max_length=64)

  • 1.

  • 2.

  • 3.

4.html

 Django queryset和字典使用_html

5.浏览

Django queryset和字典使用_html_02

Django queryset和字典使用_html_03


Django queryset和字典使用_html_04

Django queryset和字典使用_html_05

Django queryset和字典使用_html_06



Django queryset和字典使用_html_07

Django queryset和字典使用_html_08


Django queryset和字典使用_html_09

第一个obj 接收第二个obj的返回值

u1 = models.user.objects.all()

ret['racks'] = {'data': [obj for obj in u1.values()]}

print(ret)

{'racks': {'data': [{'id': 2, 'name': 'xiaoli', 'password': '766666', 'email': '2@q.com', 'sex': '2', 'c_time': datetime.datetime(2022, 4, 7, 10, 44, 39)}, {'id': 1, 'name': 'root', 'password': '123456', 'email': '1@q.com', 'sex': '1', 'c_time': datetime.datetime(2022, 4, 28, 15, 53, 45)}]}}

Django queryset和字典使用_html_10



HttpResponse("hello")             ##可以是字符串

JsonResponse({})                    ##json,不可以是字符串

return render(request, "myapp/index.html", {"foo": "bar"})

Django queryset和字典使用_html_11

Django queryset和字典使用_html_12

位置参数 (是元组)

Django queryset和字典使用_html_13

Django queryset和字典使用_html_14


Django queryset和字典使用_html_15


关键字参数(是字典)

Django queryset和字典使用_html_16

Django queryset和字典使用_html_17

Django queryset和字典使用_html_18

Django queryset和字典使用_html_19

Django queryset和字典使用_html_20

Django queryset和字典使用_html_21

Django queryset和字典使用_html_22

Django queryset和字典使用_html_23


OS执行命令

Django queryset和字典使用_html_24

Django queryset和字典使用_html_25

Django queryset和字典使用_html_26

Django queryset和字典使用_html_27


Django queryset和字典使用_html_28

Django queryset和字典使用_html_29

Django queryset和字典使用_html_30


#########################

Django queryset和字典使用_html_31


Django queryset和字典使用_html_32

Django queryset和字典使用_html_33


Django queryset和字典使用_html_34

Django queryset和字典使用_html_35

Django queryset和字典使用_html_36

Django queryset和字典使用_html_37

Django queryset和字典使用_html_38

Django queryset和字典使用_html_39

Django queryset和字典使用_html_40

Django queryset和字典使用_html_41

返回值

[{'name':'java','age':'15'},{'name':'python','age':'16'}]

Django queryset和字典使用_html_42

Django queryset和字典使用_html_43

Django queryset和字典使用_html_44

Django queryset和字典使用_html_45


Django queryset和字典使用_html_46


querydict转字典

Django queryset和字典使用_html_47

Django queryset和字典使用_html_48

Django queryset和字典使用_html_49

Django queryset和字典使用_html_50


Django queryset和字典使用_html_51

Django queryset和字典使用_html_52

Django queryset和字典使用_html_53

Django queryset和字典使用_html_54

Django queryset和字典使用_html_55


Django queryset和字典使用_html_56

Django queryset和字典使用_html_57


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。