[Basics] -2 Django ORM CRUD operations table

from django.shortcuts import render,HttpResponse,redirect
import os,sys
from django.views import View
from cmdb import models

# Create your views here.
User_dict ={
   "1":{"name":"大胖小子","age":18,"sex":"Man"},
   "2":{"name":"金虎","age":6,"sex":"Man"},
   "3":{"name":"妙音","age":5,"sex":"WoMan"},
   "4":{"name":"飞云","age":6,"sex":"Man"},
}
#主页
def index(request):
    return render(request,"index.html",{"User_dict":User_dict})


def login(request):
    if request.method == "GET":
        return render(request, "login.html")
    elif request.method == "POST":
        u=request.POST.get("user",None)
        p=request.POST.get("password",None)
        up=request.POST.get("update")
        update_msg = "更新%s密码成功!"%u
        if  models.UserInfo.objects.filter(username=u) and models.UserInfo.objects.filter(password=p):
            return render(request,'zhuye.html',{"user":u})
        if  up ==""Update:
            models.UserInfo.objects.filter(username=u).update(password=p)
            return render(request,'login.html',{'update_msg':update_msg})
        else:
            error_msg="用户名或密码错误"

    else:
        return  redirect("/index")
    return render(request, "login.html",{"error_msg":error_msg})

def zhuce(request):
    # if request.method == "GET":
    #     return render(request, "zhuce.html")
    # elif request.method == "POST":
    #     #radio
    #     radio = request.POST.get("gender")
    #     #chickbox
    #     chickbox = request.POST.getlist("favor")
    #     #select
    #     select=request.POST.getlist("city")
    #     #file
    #     send=request.FILES.get("send")
    #     print(send)
    #     file_path=os.path.join("upload",send.name)
    #     with open(file_path,mode="wb") as f:
    #         for i in send.chunks():
    #             f.write(i)
    # return render(request, "zhuce.html")
    if request.method == "GET":
        return render(request, "zhuce.html")
    elif request.method == "POST":
        username=request.POST.get("username")
        password=request.POST.get("password")
        models.UserInfo.objects.create(username=username,password=password)
        zhuce_msg = "注册成功!"
    return render(request, "zhuce.html",{"zhuce_msg":zhuce_msg})

class Home(View):

    def get(self,request):
        print(request.method)
        return render(request, "home.html")

    def post(self,request):
        u = request.POST.get("user", None)
        p = request.POST.get("pwd", None)
        if u == " The root "  and P == " 123 " :
             return the redirect ( ' / index ' )
         the else : 
            ERROR_MSG = " user name or password is incorrect " 


        return the render (Request, " home.html " , { " ERROR_MSG " : ERROR_MSG}) 

DEF Detail (request, nid): # passed over by the url parameter nid, do not have to request to acquire the value of the nid 
    detail_info = User_dict [nid]
     return the render (request, " Detail.html", { " Detail_info " : detail_info}) 

DEF ORM (Request):
     # Create 
    # of UserInfo class (database tables) for increasing the operating 
    username = request.POST.get ( " username " ) 
    password = request.POST.get ( " password " )
    # DIC = { 'username': username, 'password': password} 
  #   models.UserInfo.objects.create (** DIC) 


    # models.UserInfo.objects.create (username = username, password = password) # recommended adding data in this database 


    # obj = models.UserInfo (username = "fuyuteng", password = "123") 
    # obj.save () method to create a second data 

    '' 'Find all '' ' 
    # the Result = models.UserInfo.objects.all () 
    ' '' filter query username = root of all the conditions in line with the query '' ' 
    the Result = models.UserInfo.objects.filter (username = ' root ' )
     '' ' deleted ' '' 
    the delete = models.UserInfo.objects.filter (username = username, password = password) .Delete () # delete the specified delete = models.UserInfo.objects.all (). delete ( ) # delete All 
    '' ' update ' '' 
    models.UserInfo.objects.update (password = password) # update the password for all password 
    update = models.UserInfo.objects.filter (username = username) .update(password=password)#Updated password for the username specified 

    return the render (Request, ' orm.html ' , { ' Result ' :} Result)

Guess you like

Origin www.cnblogs.com/fuyuteng/p/12217593.html