一、首先配置orm
1、首先在settings.py中配置(数据库需要手动提前先建好)
注释以下内容(47行)
# 'django.middleware.csrf.CsrfViewMiddleware',
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'orm_db1', 'HOST':'127.0.0.1', 'PORT':3306, 'USER':'root', 'PASSWORD':'mariadb.123', } }
2、在__init__.py中添加以下内容:
import pymysql pymysql.install_as_MySQLdb()
二、在项目中配置
1、在models.py中创建用户表与字段填写
from django.db import models class User(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=32) password = models.CharField(max_length=32) address = models.CharField(max_length=32) phone=models.CharField(max_length=64,default='120') #______________ 分割线,另做其他使用___________________________ class Publish(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=32) email = models.EmailField() addr = models.CharField(max_length=64) class Author(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=32) address = models.CharField(max_length=32) class Book(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=32) # 21.38 price = models.DecimalField(max_digits=5, decimal_places=2) # 一对多的关系确立,关联字段写在多的一方,orm自动在publish后面加id,publish_id publish = models.ForeignKey(to='Publish', to_field='id') # 多对多关系,orm会自动创建第三张表 authors = models.ManyToManyField(to='Author')
1 python3 manage.py makemigrations ----记录一下数据库的变化
2 python3 manage.py migrate ----将变化同步到数据库中
2、在总路由添加以后可能要添加的功能路由地址与对应的视图
from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^user_list/',views.user_list), url(r'^delete/',views.delete), url(r'^add/',views.add), url(r'^edit/',views.edit), ]
3、在视图功能文件中填写:views.py
from django.shortcuts import render,HttpResponse,redirect from app01 import models # Create your views here. def user_list(request): if request.method=='GET': # 查询user表中所有数据.--相当于sql select * from app01_user; # 返回结果是QuerySet对象(先当成列表) # [user1,user2] # 单表查询所有用户 ret=models.User.objects.all() # print(type(ret)) # for i in ret: # print(type(i)) # print(i.name) return render(request,'user_list.html',{'user_list':ret}) def delete(request): # get请求携带参数:http://127.0.0.1:8000/deleteuser/?id=1 if request.method=='GET': # 后台取值:request.GET.get('id') id=request.GET.get('id') # orm删除记录 models.User.objects.filter(id=id).delete() return redirect('/user_list/') def add(request): if request.method=='GET': return render(request, 'add.html') elif request.method=="POST": # 前台post提交的数据取值: name=request.POST.get('name') pwd=request.POST.get('password') addr=request.POST.get('addr') phe=request.POST.get('phone_a') # 方式一 # user=models.User(name=name,password=pwd,address=addr) # user.save() #写入数据库 # 方式二 models.User.objects.create(name=name,password=pwd,address=addr,phone=phe) return redirect('/user_list/') def edit(request): if request.method=='GET': id=request.GET.get('id') # orm查询单条数据 user=models.User.objects.filter(id=id).first() return render(request,'edit.html',{'user':user}) if request.method=='POST': id=request.POST.get("id") name = request.POST.get('name') pwd = request.POST.get('password') addr = request.POST.get('addr') phe = request.POST.get('phone_a') # orm的修改 models.User.objects.filter(id=id).update(name=name,password=pwd,address=addr,phone=phe) return redirect('/user_list/')
4、在templates目录中创建以下html文件
add.html
edit.html
user_list.html
add.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>新增用户</title> </head> <body> <form action="" method="post"> <p>用户名: <input type="text" name="name"></p> <p>密码: <input type="password" name="password"></p> <p>地址: <input type="text" name="addr"></p> <p>电话:<input type="text" name="phone_a"></p> <input type="submit" value="提交"> </form> </body> </html>
edit.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/edit/?id={{ user.id }}" method="post"> <p><input type="hidden" name="id" value="{{ user.id }}"></p> <p>用户名: <input type="text" name="name" value="{{ user.name }}"></p> <p>密码: <input type="text" name="password" value="{{ user.password }}"></p> <p>地址: <input type="text" name="addr" value="{{ user.address }}"></p> <p>电话:<input type="text" name="phone_a" value="{{ user.phone }}"></p> <input type="submit" value="提交"> </form> </body> </html>
user_list.html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1"> <thead> <tr> <th>id</th> <th>name</th> <th>password</th> <th>address</th> <th>phone</th> <th>delete</th> <th>add</th> <th>edit</th> </tr> </thead> <tbody> {% for user in user_list %} <tr> <td>{{ user.id }}</td> <td>{{ user.name }}</td> <td>{{ user.password }}</td> <td>{{ user.address }}</td> <td>{{ user.phone }}</td> <td><a href="/delete?id={{ user.id }}">删除</a> </td> <td><a href="/edit?id={{ user.id }}">编辑</a></td> <td><a href="/add/">新增用户</a></td> </tr> {% endfor %} </tbody> </table> </body> </html>
总结:
1 orm 创建表,新增字段,修改,删除字段,不能创建数据库 -字段属性phone=models.CharField(max_length=64,null=True) -null=True 代表该列可以为空 2 数据的增删改查 -增(1):User.objects.create(address='') -增(2):实例化产生一个user对象,user对象的save方法 -删:User.objects.filter(id=1,name='lqz').first().delete() -删:User.objects.filter(id=1,name='lqz').delete() -改:User.objects.filter(id=1,name='lqz').update() -查:User.objects.all() user=User.objects.filter(name='lqz') user.name
3 前后台交互 -id=1&name='lqz'& <form action="/updateuser/?id={{ user.id }}" method="post"> <p><input type="hidden" name="id" value="{{ user.id }}"></p> <p>用户名: <input type="text" name="name" value="{{ user.name }}"></p> <p>密码: <input type="text" name="password" value="{{ user.password }}"></p> <p>地址: <input type="text" name="addr" value="{{ user.address }}"></p> <input type="submit" value="提交"> </form>
4 django生命周期 5 一对多,多对多 -publish_id -publish=models.ForeignKey(to='Publish', to_field='id') book.publish_id book.publish 多对多:(自动创建第三张表) authors = models.ManyToManyField(to='Author')
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。