• Python的Django


    1   第一部分目录详解

    修改django的项目当中的url中的配置:

    from django.contrib import admin
    from django.conf.urls import url
    from django.urls import path
    from django.shortcuts import HttpResponse
    def home(request):
        return HttpResponse('<h1>hello</h1>')
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^h.html/', home),
    ]
    View Code

     需要输入对应的页面才可以访问

     #2 部分   创建APP

         建议做django的时候  在比较干净的目录做,不要目录嵌套目录

    创建app

    D:DocumentPython0404Django0425>python manage.py startapp cmdb

    D:DocumentPython0404Django0425>python manage.py startapp openstack

    创建完后进行一定的修改,将之前放在根下的url文件中内容进行修改:

    修改如下:D:DocumentPython0404Django0425Django0425urls.py

    from django.contrib import admin
    from django.conf.urls import url
    from django.urls import path
    from cmdb import views
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^h.html/', views.home),
    ]
    

      

    修改D:DocumentPython0404Django0425cmdbviews.py

    from django.shortcuts import HttpResponse
    def home(request):
        return HttpResponse('<h1>hello This is CMDB</h1>')
    

      

    23:03  基本的用户名密码操作  实现简单的用户名密码登录交互界面

    urls.py

    """Django0425 URL Configuration
    
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/2.0/topics/http/urls/
    Examples:
    Function views
        1. Add an import:  from my_app import views
        2. Add a URL to urlpatterns:  path('', views.home, name='home')
    Class-based views
        1. Add an import:  from other_app.views import Home
        2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
    Including another URLconf
        1. Import the include() function: from django.urls import include, path
        2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
    """
    from django.contrib import admin
    from django.conf.urls import url
    from django.urls import path
    from cmdb import views
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^h.html/', views.home),
    url(r'^login', views.login),     #注意此处不要有/
    ]

    login.html

     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             width: 80px;
    10             text-align: right;
    11             display: inline-block;
    12         }
    13     </style>
    14 </head>
    15 <body>
    16 <form action="/login" method="post">
    17     <p>
    18         <label for="username">用户名:</label>
    19         <input id="username" type="test" name="user"/>
    20     </p>
    21     <p>
    22         <label for="password">密码:</label>
    23         <input id="password" type="test" name="pwd"/>
    24         <input type="submit" value="提交"/>
    25         <span>
    26         {{ error_msg }}
    27     </span>
    28 
    29     </p>
    30 </form>
    31 <script src="/static/jquery-1.12.4.js"></script>
    32 </body>
    33 </html>
    View Code

    views.py

     1 from django.shortcuts import render
     2 
     3 # Create your views here.
     4 from django.shortcuts import HttpResponse
     5 
     6 from django.shortcuts import render
     7 from django.shortcuts import redirect
     8 
     9 def login(request):
    10     #包含用户提交的所有信息
    11     # f=open('templates/login.html','r',encoding='utf-8')
    12     # data=f.read()
    13     # f.close()
    14     # return HttpResponse(data)
    15 
    16     print(request.method)   #获取用户的提交方式
    17     error_msg=''
    18     if request.method=="POST":
    19         #1 普通写法
    20         # user=request.POST['user']
    21         # pwd=request.POST['pwd']
    22         #2 较好的写法  相比较第一种如果获取不到会报错 第二种
    23         #不会
    24         user=request.POST.get('user',None)
    25         pwd=request.POST.get('pwd',None)
    26         print('33[31;1muserinfo:%s33[0m'%user)
    27         print('33[33;1mpasswdinfo:%s33[0m'%pwd)
    28         if user=='nod' and pwd=='nod':
    29             #引入redirect重定向
    30             return redirect('http://www.baidu.com')
    31         else:
    32             error_msg='你输入的账户密码不正确,请重新输入'
    33 
    34     return render(request,'login.html',{'error_msg':error_msg})
    35 
    36 def home(request):
    37     return HttpResponse('<h1>hello This is CMDB</h1>')
    View Code

    注意settings里需要修改的配置

  • 相关阅读:
    springmvc乱码问题
    51nod 还是01串
    51nod 1276 岛屿的数量
    poj 2486 a apple tree
    hdu 1011 Starship Troopers
    poj 1155 TELE
    hdu 4586 Play the Dice
    hdu 5023 A Corrupt Mayor's Performance Art(线段树水题)
    Appleman and Tree
    hdu 4003
  • 原文地址:https://www.cnblogs.com/nodchen/p/8948265.html
Copyright © 2020-2023  润新知