[TOC=1,5] >[success] # 简单的登录案例 >[danger] ##### views 层案 ~~~ def login(request): msg="" if request.method == "POST": user = request.POST.get("user",None) pwd = request.POST.get('pwd',None) if user == "123" and pwd == "456": return redirect("https://www.baidu.com") else: msg="用户密码错误" return render(request,'index.html',{'msg':msg}) return render(request,'index.html',{'msg':msg}) ~~~ >[danger] ##### templates 层html ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1> 欢迎登录 </h1> <form method="post"> <input type="text" name="user"> <input type="text" name="pwd"> <span style="color: red">{{ msg }}</span> <input type="submit"> </form> </body> </html> ~~~ >[success] # 简单的逻辑交互 >[danger] #### views 层案例 ~~~ USERLIST = [ {"user":"w","pwd":"123"}, ] def login(request): if request.method == "POST": user = request.POST.get('user','') pwd = request.POST.get('pwd','') USERLIST.append({'user':user,"pwd":pwd}) return render(request,'index.html',{'USERLIST':USERLIST}) ~~~ >[danger] #### templates 层交互 ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1> 欢迎登录 </h1> <form method="post"> <input type="text" name="user"> <input type="text" name="pwd"> <input type="submit"> </form> {% for i in USERLIST %} {{ i.user }}-{{ i.pwd }} {% endfor %} </body> </html> ~~~