多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC=1,5] >[success] # 简单的数据增删改查 >[info] ## 用到的知识点 ~~~ 1.obj = models.UserInfo.objects.filter(user=user,pwd=pwd) #查询用户 2.models.UserInfo.objects.create(user=user,pwd=pwd) #增加用户 3.obj = models.UserInfo.objects.filter(id=nid).first() #first 获取第一个用户 4.models.UserInfo.objects.filter(id=nid).delete() #删除 ~~~ >[danger] ##### urls.py 文件 ~~~ from django.conf.urls import url from . import views urlpatterns = [ url(r'login.html/',views.login), url(r'register.html/',views.register), url(r'index.html/',views.index), url(r'userinfo-(?P<nid>(\d+))/',views.userinfo), url(r'deleinfo-(?P<nid>(\d+))/',views.deleinfo), ] ~~~ >[danger] ##### views ~~~ from django.shortcuts import render,redirect from . import models # Create your views here. def login(request): msg = "" if request.method == "GET": return render(request,'login.html',{"msg":msg}) if request.method == "POST": user = request.POST.get('user',None) pwd = request.POST.get('pwd',None) if user and pwd: # 查询用户名密码 obj = models.UserInfo.objects.filter(user=user,pwd=pwd) if obj: return redirect("/index.html/") msg = "用户密码不能为空" return render(request, 'login.html', {"msg": msg}) def register(request): msg = "" if request.method =="GET": return render(request,"register.html",{"msg":msg}) elif request.method == "POST": user = request.POST.get('user',None) pwd = request.POST.get('pwd',None) if user and pwd: # 创建用户名密码 models.UserInfo.objects.create(user=user,pwd=pwd) return redirect('/login.html/') msg = "用户名密码不能为空" return render(request,'register.html',{"msg":msg}) def index(request): if request.method == "GET": # 展示用户名密码 obj = models.UserInfo.objects.all() return render(request,"index.html",{'obj':obj}) def userinfo(request,nid): if request.method == "GET": # 展示单个用户名密码 obj = models.UserInfo.objects.filter(id=nid).first() return render(request,"userinfo.html",{'obj':obj}) def deleinfo(request,nid): if request.method == "GET": # 删除用户名密码 models.UserInfo.objects.filter(id=nid).delete() return redirect("/index.html/") ~~~