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


     1 """S14Djngo URL Configuration
     2 
     3 The `urlpatterns` list routes URLs to views. For more information please see:
     4     https://docs.djangoproject.com/en/1.11/topics/http/urls/
     5 Examples:
     6 Function views
     7     1. Add an import:  from my_app import views
     8     2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
     9 Class-based views
    10     1. Add an import:  from other_app.views import Home
    11     2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
    12 Including another URLconf
    13     1. Import the include() function: from django.conf.urls import url, include
    14     2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
    15 """
    16 from django.conf.urls import url
    17 from django.contrib import admin
    18 
    19 from cmdb import views
    20 
    21 
    22 
    23 
    24 urlpatterns = [
    25     url(r'^admin/', admin.site.urls),
    26     #url(r'^h.html/',views.home),
    27     url(r'^login',views.login),
    28     url(r'^home', views.home),
    29 ]
    urls.py
     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6     <link rel="stylesheet" href="/static/commons.css"/>
     7     <style>
     8         label{
     9             80px;
    10             text-align:right;
    11             display: inline-block;
    12         }
    13     </style>
    14 </head>
    15 <body>
    16 
    17     <form action="/login" method="post">
    18         <p>
    19             <label for="username">用户名:</label>
    20             <input id="username" name="user" type='text'/>
    21         </p>
    22         <p>
    23              <label for="password">密码:</label>
    24              <input id="password" name="pwd" type='password'/>
    25              <input type="submit" value="提交"/>
    26             <span style="color:red;">{{ error_msg }}</span>
    27         </p>
    28     </form>
    29     <script src ="/static/jquery.min.js"></script>
    30 </body>
    31 </html>
    login.html
     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         <table>
    13             <tr>
    14                 <td>李杰</td>
    15                 <td>男</td>
    16                 <td>sra@126.com</td>
    17             </tr>
    18             <tr>
    19                 <td>研行</td>
    20                 <td>女</td>
    21                 <td>dgdg@163.com</td>
    22             </tr>
    23             <tr>
    24                 <td>天天</td>
    25                 <td>中</td>
    26                 <td>sfsf@139.com</td>
    27             </tr>
    28         </table> 
    29     </div>
    30 
    31 </body>
    32 </html>
    home.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 def home(request):
    28     return render(request,'home.html')
    views.py


    用JetBrains PyCharm 2017.2创建运行Django程序
    http://www.cnblogs.com/ujq3/p/7882030.html

    创建多个Django业务模块
    http://www.cnblogs.com/ujq3/p/7884075.html

    Django多业务模块的写法
    http://www.cnblogs.com/ujq3/p/7884279.html

    Django与HTML业务基本结合--基本的用户名密码提交方法2
    http://www.cnblogs.com/ujq3/p/7884615.html

    Django静态文件以及模板文件的配置
    http://www.cnblogs.com/ujq3/p/7884881.html

    Django用户名密码错误提示
    http://www.cnblogs.com/ujq3/p/7891352.html

  • 相关阅读:
    MySQLselect查询
    MySQL备份数据库
    MySQLdelete语句
    MySQL数据库指令
    MySQLupdate语句
    MySQLinsert插入语句
    MySQL表的操作:创建表、修改表、删除表
    我的收藏周刊009
    我的收藏周刊008
    我的收藏周刊010
  • 原文地址:https://www.cnblogs.com/ujq3/p/7891774.html
Copyright © 2020-2023  润新知