Python - Django - ORM 操作数据
查询数据(查询管理员):
app01/models.py 中定义的类,也就是创建的表
from django.db import models # 类必须继承 models.Model class Admin(models.Model): # 创建一个主键自增的字段 id = models.AutoField(primary_key=True) # AutoField 为自增的字段 # 创建一个 varchar 类型的不能为空的字段 # varchar 类型需要指定最大长度 username = models.CharField(null=False, max_length=20) password = models.CharField(null=False, max_length=20)
admin 表中的数据
在 app01/views.py 中写上获取数据的函数
from django.shortcuts import render from app01 import models def admin_list(request): admins = models.Admin.objects.all() # 获取所有数据 print(admins[0].id, admins[0].username, admins[0].password) return render(request, "admin_list.html", {"admin_list": admins})
然后在 mysite/urls.py 中写上对应关系
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" from django.conf.urls import url from django.shortcuts import HttpResponse, render, redirect from app01 import views # 从 app01 中导入函数 # 保存路径和函数的对应关系 urlpatterns = [ url(r'^admin_list/', views.admin_list) ]
admin_list.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>管理员列表</title> </head> <body> <table border="1"> <thead> <tr> <th>id</th> <th>用户名</th> <th>密码</th> </tr> </thead> <tbody> {% for user in admin_list %} <tr> <td>{{ user.id }}</td> <td>{{ user.username }}</td> <td>{{ user.password }}</td> </tr> {% endfor %} </tbody> </table> </body> </html>
这里用到了 for 循环来读取数据
{% for user in admin_list %} <tr> <td>{{ user.id }}</td> <td>{{ user.username }}</td> <td>{{ user.password }}</td> </tr> {% endfor %}
运行结果
添加数据(添加管理员):
在 admin_list.html 中增加一个添加管理员的选项
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>管理员列表</title> </head> <body> <table border="1"> <thead> <tr> <th>id</th> <th>用户名</th> <th>密码</th> </tr> </thead> <tbody> {% for user in admin_list %} <tr> <td>{{ user.id }}</td> <td>{{ user.username }}</td> <td>{{ user.password }}</td> </tr> {% endfor %} </tbody> </table> <a href="/add_admin/">添加管理员</a> </body> </html>
点击后会跳转到 /add_admin/ 进行相关的操作
add_admin.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加管理员</title> </head> <body> <form action="/add_admin/" method="post"> <p>用户名: <input type="text" name="username"> </p> <p>密码: <input type="text" name="password"> </p> <p> <input type="submit" value="提交"> </p> </form> </body> </html>
app01/views.py 下的代码
from django.shortcuts import render, redirect from app01 import models def admin_list(request): admins = models.Admin.objects.all() # 获取所有数据 print(admins[0].id, admins[0].username, admins[0].password) return render(request, "admin_list.html", {"admin_list": admins}) def add_admin(request): if request.method == "POST": user = request.POST.get("username", None) pwd = request.POST.get("password", None) # 去数据库中新增一条数据 models.Admin.objects.create(username=user, password=pwd) # 添加成功后跳转 return redirect("/admin_list/") return render(request, "add_admin.html")
在 mysite/urls.py 中添加对应关系
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" from django.conf.urls import url from django.contrib import admin from django.shortcuts import HttpResponse, render, redirect from app01 import views # 保存路径和函数的对应关系 urlpatterns = [ url(r'^admin_list/', views.admin_list), url(r'^add_admin/', views.add_admin) ]
运行结果:
点击“添加管理员”
输入用户名和密码,点击“提交”
管理员添加成功