Django用户登陆以及跳转后台管理页面3

 

Django用户登陆以及跳转后台管理页面1
http://www.cnblogs.com/ujq3/p/7891774.html

Django用户登陆以及跳转后台管理页面2
http://www.cnblogs.com/ujq3/p/7891901.html

 

 


 

 

 1 from django.shortcuts import render
 2 # Create your views here.
 3 
 4 from django.shortcuts import render
 5 from django.shortcuts import redirect  #重新定向模块
 6 
 7 def login(request):
 8      #包含用户提交的所有信息
 9      #获取用户提交方法
10     #print(request.method)
11     error_msg = ""
12     if request.method == "POST":
13         #获取用户通过POST提交过来的数据
14         user =request.POST.get('user',None)
15         pwd =request.POST.get('pwd',None)
16         if user == 'root' and pwd == '123':
17             #去跳转到
18             return  redirect('home.html')
19         else:
20             #用户密码不匹配
21             error_msg = '用户名或密码错误'
22         # user = request.POST['user']
23         # pwd = request.POST['pwd']
24         # print(user,pwd)
25     return render(request,'login.html',{'error_msg':error_msg})
26 
27 USER_LIST =[
28     {'username': 'alex', 'email': 'alex@163.com', 'gender': ''},
29     {'username': '黄逗', 'email': 'huandou@126.com', 'gender': ''},
30     {'username': '小玉123', 'email': 'xiaoyu@139.com', 'gender': ''},
31 ]
32 
33 def home(request):
34     if request.method == "POST":
35         #获取用户添加的数据 POST请求中
36         u = request.POST.get('username')
37         e = request.POST.get('email')
38         g = request.POST.get('gender')
39         temp = {'username':u, 'email':e, 'gender':g}
40         USER_LIST.append(temp)
41     return render(request,'home.html',{'user_list':USER_LIST})
views.py

 

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7     <meta charset = "utf-8">
 8     <title>Title</title>
 9 <body style="margin: 0">
10     <div style="height: 48px;background-color: #ddb796"></div>
11     <div>
12         <form action="/home"method="post">
13             <input type="text" name="username" placeholder="用户名"/>
14             <input type="text" name="email" placeholder="邮箱"/>
15             <input type="text" name="gender" placeholder="性别"/>
16             <input type="submit" value="添加" />
17         </form>
18     </div>
19     <div>
20         <table>
21             {% for row in user_list %}
22                 <tr>
23                     <td>{{ row.username }}</td>
24                     <td>{{ row.email }}</td>
25                     <td>{{ row.gender }}</td>
26                 </tr>
27             {% endfor %}
28         </table> 
29     </div>
30 
31 </body>
32 </html>
home.html

 

 

posted @ 2017-11-24 19:16  颜言  阅读(411)  评论(0编辑  收藏  举报