“Django入库”的版本间的差异
跳到导航
跳到搜索
第1行: | 第1行: | ||
=django数据入库= | =django数据入库= | ||
+ | |||
+ | ==1、声明类== | ||
<pre> | <pre> | ||
1、声明类: | 1、声明类: | ||
第6行: | 第8行: | ||
e = Employee() | e = Employee() | ||
e.name = '入库数据' | e.name = '入库数据' | ||
− | e.save()即可存入 | + | e.save()即可存入</pre> |
− | |||
− | |||
+ | ==2、直接类中使用:== | ||
+ | <pre> | ||
ee = Employee(name='aa') | ee = Employee(name='aa') | ||
ee.save() | ee.save() | ||
− | 3、自带返回值: | + | ==3、自带返回值:== |
+ | <pre> | ||
+ | Employee.objects.create(name='字段值') | ||
+ | |||
+ | Django是在views.py文件中,通过导入models.py文件来创建数据的: | ||
+ | |||
+ | from django.shortcuts import render | ||
+ | |||
+ | # Create your views here. | ||
+ | from blog import models #导入blog模块 | ||
+ | from django.shortcuts import HttpResponse | ||
+ | def db_handle(request): | ||
+ | models.UserInfo.objects.create(username='andy',password='123456',age=33) | ||
+ | return HttpResponse('OK') | ||
+ | |||
− | |||
2021年7月3日 (六) 02:48的版本
django数据入库
1、声明类
1、声明类: e = Employee() e.name = '入库数据' e.save()即可存入
2、直接类中使用:
ee = Employee(name='aa') ee.save() ==3、自带返回值:== <pre> Employee.objects.create(name='字段值') Django是在views.py文件中,通过导入models.py文件来创建数据的: from django.shortcuts import render # Create your views here. from blog import models #导入blog模块 from django.shortcuts import HttpResponse def db_handle(request): models.UserInfo.objects.create(username='andy',password='123456',age=33) return HttpResponse('OK') 查询: 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 %}