“Django入库”的版本间的差异
跳到导航
跳到搜索
第1行: | 第1行: | ||
+ | |||
+ | =django数据入库= | ||
+ | <pre> | ||
+ | 1、声明类: | ||
+ | |||
+ | e = Employee() | ||
+ | e.name = '入库数据' | ||
+ | e.save()即可存入 | ||
+ | |||
+ | 2、直接类中使用: | ||
+ | |||
+ | ee = Employee(name='aa') | ||
+ | ee.save() | ||
+ | |||
+ | 3、自带返回值: | ||
+ | |||
+ | Employee.objects.create(name='字段值') | ||
+ | |||
+ | |||
+ | 查询: | ||
+ | |||
+ | result = Employee.objects.all() | ||
+ | |||
+ | 返回数据对象 | ||
+ | |||
+ | |||
+ | |||
+ | 直接在页面中输出: | ||
+ | |||
+ | from blog.models import Employee | ||
+ | from django.shortcuts import render_to_response | ||
+ | def index(req): | ||
+ | a= Employee.objects.all() | ||
+ | return render_to_response('index.html',{'items':a}) | ||
+ | |||
+ | |||
+ | 页面视图: | ||
+ | |||
+ | {% for i in items%} | ||
+ | <div>{{i}}</div> | ||
+ | {% endfor %} | ||
+ | </pre> | ||
+ | |||
=see also= | =see also= | ||
+ | [https://blog.csdn.net/fishermanmax/article/details/53233277 python-django的数据简单入库] | ||
+ | |||
[https://www.cnblogs.com/alphajx/p/5097114.html Django ORM 中的批量操作] | [https://www.cnblogs.com/alphajx/p/5097114.html Django ORM 中的批量操作] | ||
2021年7月3日 (六) 02:43的版本
django数据入库
1、声明类: e = Employee() e.name = '入库数据' e.save()即可存入 2、直接类中使用: ee = Employee(name='aa') ee.save() 3、自带返回值: Employee.objects.create(name='字段值') 查询: result = Employee.objects.all() 返回数据对象 直接在页面中输出: from blog.models import Employee from django.shortcuts import render_to_response def index(req): a= Employee.objects.all() return render_to_response('index.html',{'items':a}) 页面视图: {% for i in items%} <div>{{i}}</div> {% endfor %}