多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC=1,5] >[success] # DBFIRST 和 CODEFIRST ~~~ DBFIRST:先创建数据库表结果,在生成类,根据类操作数据库 CODEFIRST:创建类,根据类创建数据库表,根据类对数据库中的数据进行操作 Django是属于codefirst类型 ~~~ >[success] # Django 增删改查 >[danger] ##### 增-creat `models.表名.objects.creat(字段="内容",)` ~~~ obj = models.表名( 字段="内容" ) obj.save() ~~~ >[danger] ##### 删-delete `models.UserInfo.objects.filter(id='2').delete()` >[danger] ##### 改-update `models.表名.objects.filter(id='2').update(password = "1234")` >[danger] ##### 查-fittler `models.表名.objects.fittler(字段="内容")` >[danger] ##### 打印sql -query `models.表名.objects.filter(id=nid).query` * * * * * </br> >[success] # 简单的登录注册案例 >[danger] ##### models.py 文件配置 `执行前在setting中注册好app` ~~~ from django.db import models # Create your models here. class UserInfo(models.Model): username = models.CharField(max_length=32) password = models.CharField(max_length=32) ~~~ >[danger] ##### urls 配置 ~~~ from django.conf.urls import url from . import views urlpatterns = [ url(r'register.html/$',views.register), url(r'login.html/$',views.login), ] ~~~ >[danger] ##### 注册的Views层 ~~~ def register(request): msg = "" if request.method == "GET": return render(request,'register.html',{'msg':msg}) # Post 访问时候执行 if request.method == "POST": username = request.POST.get('username',None) pwd = request.POST.get('pwd',None) # 判断用户名密码不为空 if (pwd and username): # 创建用户名密码 --creat models.UserInfo.objects.create(password=pwd,username=username) return redirect('/login.html') return render(request,'register.html',{'msg':'用户名密码不能为空'}) ~~~ >[danger] ##### 登录的Views层 ~~~ def login(request): msg = "" if request.method == "GET": return render(request, 'register.html', {'msg': msg}) # POST请求时候 if request.method == "POST": username = request.POST.get('username', None) pwd = request.POST.get('pwd', None) # 查询用户名密码是否存在 -- fittler UserInfo = models.UserInfo.objects.filter(username=username,password=pwd) if UserInfo: return redirect("https://www.baidu.com") return render(request, 'register.html', {'msg': '用户名密码不能为空'}) ~~~