Django--model模型绑定_数据库操作
1、创建model类
app01/models.py
1 2 3 4 5 6 7 | from django.db import models# Create your models here.class UserInfo(models.Model): #创建model类,必须继承自models.Model类 # 设计表结构和字段,最大长度,默认表名app01_userinfo email = models.CharField(max_length=16) pwd = models.CharField(max_length=20) |
2、注册app
settings.py
1 2 3 4 5 6 7 8 9 | INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01',] |
3、执行命令创建表
cmd> python manage.py makemigrations 生成源信息,一个字典,位置:app01/migrations/0001_initial.py
cmd> python manage.py migrate 读取生成的源信息,去生成数据库表
4、查看
4.1、用navicat-SQLite工具查看

4.2、用程序查看
app01/admin.py
1 2 | from app01 import modelsadmin.site.register(models.UserInfo) #把UserInfo创建的表注册进admin页面 |
浏览器访问http://127.0.0.1:8000/admin/
点进去后就可以添加用户
5、现在有一个用户user1,登录成功后跳转到index页面,列出数据库表里的所有用户信息。
app01/views.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from django.shortcuts import renderfrom django.shortcuts import redirectdef login(request): if request.method == 'POST': input_email = request.POST['email'] input_pwd = request.POST['pwd'] if input_email == 'user1@qq.com' and input_pwd == '123': return redirect("/index/") #登录成功后跳转到index页面 else: return render(request,"login.html",{"status":"用户名密码错误"}) return render(request,'login.html')def index(request): #从数据库中获取数据,并和html渲染 from app01 import models #获取表中所有数据 user_info_list = models.UserInfo.objects.all() return render(request,'index.html',{"user_info_list":user_info_list,}) |
urls.py
1 2 3 4 5 6 7 8 | from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/', views.login), url(r'^index/', views.index),] |
templates/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title></head><body> <div> <table> <thead> <tr> <th>邮箱</th> <th>密码</th> </tr> </thead> <tbody> {% for line in user_info_list %} <tr> <td>{{ line.email }}</td> <td>{{ line.pwd }}</td> </tr> {% endfor %} </tbody> </table> </div></body></html> |
浏览器访问 http://127.0.0.1:8000/login/
登录成功后跳转到 http://127.0.0.1:8000/index/,列出所有的刚才手动在页面创建的用户。

6、model添加数据
index.html
1 2 3 4 5 | <form action="/index/" method="post"> <input type="text" name="em" /> <input type="text" name="pw" /> <input type="submit" value="添加" /></form> |
views.py
1 2 3 4 5 6 7 8 9 10 11 | def index(request): #从数据库中获取数据,并和html渲染 from app01 import models #添加数据 if request.method=='POST': input_em = request.POST['em'] input_pw = request.POST['pw'] models.UserInfo.objects.create(email=input_em,pwd=input_pw) #获取表中所有数据 user_info_list = models.UserInfo.objects.all() return render(request,'index.html',{"user_info_list":user_info_list,}) |
7、model删除数据
index.html
1 2 3 4 5 | <form action="/index/" method="post"> <input type="text" name="email_del"> <input type="text" name="pwd_del"> <input type="submit" value="删除"></form> |
views.py
1 2 3 4 5 6 | def index(request): from app01 import models #删除数据 if request.method=='POST': input_email = request.POST['email_del'] models.UserInfo.objects.filter(email=input_email).delete() |
8、model更新数据
index.html
1 2 3 4 5 | <form action="/index/" method="post"> <input type="text" name="email_update"> <input type="text" name="pwd_update"> <input type="submit" value="更新"></form> |
views.py
1 2 3 4 5 6 | def index(request): #更新数据 if request.method=='POST': input_em = request.POST['email_update'] input_pw = request.POST['pwd_update'] models.UserInfo.objects.filter(email=input_em).update(pwd=input_pw) |





浙公网安备 33010602011771号