Python的Django

 

 

1   第一部分目錄詳解css

 

修改django的項目當中的url中的配置:html

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

 

 

 須要輸入對應的頁面才能夠訪問python

 

 

 

 

 

 

 #2 部分   建立APPjquery

 

     建議作django的時候  在比較乾淨的目錄作,不要目錄嵌套目錄django

 

建立appapp

D:\Document\Python0404\Django0425>python manage.py startapp cmdbide

D:\Document\Python0404\Django0425>python manage.py startapp openstackpost

 

 

建立完後進行必定的修改,將以前放在根下的url文件中內容進行修改:url

修改以下:D:\Document\Python0404\Django0425\Django0425\urls.pyspa

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:\Document\Python0404\Django0425\cmdb\views.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('\033[31;1muserinfo:%s\033[0m'%user)
27         print('\033[33;1mpasswdinfo:%s\033[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裏須要修改的配置

相關文章
相關標籤/搜索