上面咱們已經知道Python的WEB框架有Django、Tornado、Flask 等多種,Django相較與其餘WEB框架其優點爲:大而全,框架自己集成了ORM、模型綁定、模板引擎、緩存、Session等諸多功能。今天就一塊兒來學習下Django;css
1)打開cmd,進入到python安裝路徑下的Scripts; 2)使用pip安裝:pip install Django 3)添加環境變量:python安裝路徑下的Scripts;
目錄結構以下:html
2. 配置文件python
1)數據庫:mysql
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME':'dbname', 'USER': 'root', 'PASSWORD': 'xxx', 'HOST': '', 'PORT': '', } }
2)模板:sql
TEMPLATE_DIRS = ( os.path.join(BASE_DIR,'templates'), )
3)靜態文件:數據庫
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'static'), )
# 切換到Django項目目錄,執行命令 python manage.py startapp cmdb # 目錄結構 - cmdb - migrations #數據庫操做記錄(只是修改表結構的記錄) - init #表示python數據包(python3中有無都可) - admin #Django爲咱們提供的後臺管理 - apps #配置當前app - models #建立數據庫表結構,寫指定的類,經過命令能夠建立數據庫結構 - tests #單元測試 - views #寫業務邏輯代碼,最重要的就是這個文件了
1)登陸實例django
templates下生成html文件,如login.html瀏覽器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> label{ width: 80px; text-align: right; display: inline-block; } </style> <link rel="stylesheet" href="/static/commons.css"/> </head> <body> <form action="/login/" method="post"> <p> <label for="username">用戶名:</label> <input id="username" type="text" name="user"/> </p> <p> <label for="password">密碼:</label> <input id="password" type="text" name="pwd"/> <input type="submit" value="提交" style="cursor:pointer"/> <span style="color: red;font-size:15px;">{{error_msg}}</span> </p> </form> </body> </html>
修改url文件,定義路由規則緩存
from django.conf.urls import url from django.contrib import admin from cmdb import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login', views.login), ]
定義視圖函數:app下views.pyapp
from django.shortcuts import render # Create your views here. from django.shortcuts import HttpResponse from django.shortcuts import render from django.shortcuts import redirect import time USER_LIST=[ {'username':'alex','email':'lei10@qq.com','gender':'男'}, {'username':'cc','email':'lei12@qq.com','gender':'男'}, {'username':'tt','email':'lei13@qq.com','gender':'女'} ] def home(request): if request.method=="POST": u = request.POST.get("username"); e = request.POST.get("email"); g = request.POST.get("gender"); temp = {'username':u,'email':e,"gender":g} USER_LIST.append(temp) return render(request,"home.html",{"user_list":USER_LIST}) def login(request): error_msg="" if request.method=="POST": user = request.POST.get('user',None); pwd = request.POST.get('pwd',None); if user=="root" and pwd=="111111": return redirect("/home/") elif user=="": error_msg="用戶名不能爲空!" elif pwd == "": error_msg = "密碼不能爲空!" else: error_msg="用戶名或密碼錯誤!"; return render(request,"login.html",{'error_msg':error_msg})
瀏覽器訪問http://127.0.0.1:8000/login顯示login.html寫的登陸頁面,可輸入登陸信息查看頁面顯示
經過上面的例子咱們能夠知道Django的生命週期:
-> URL對應關係(匹配) -> 視圖函數 -> 返回用戶字符串
-> URL對應關係(匹配) -> 視圖函數 -> 打開一個HTML文件,讀取內容
2)其餘
request.GET.get('',None) # 獲取get請求發來的數據 request.POST.get('',None) # 獲取post請求發來的數據 return HttpResponse("字符串") return render(request, "HTML模板的路徑") return redirect('/只能填URL')
上面實例中的login.html就是模板;對於模版,其實就是讀取模版(其中嵌套着模版標籤),而後將 Model 中獲取的數據插入到模版中,最後將信息返回給用戶。
模板中也有本身的語言,該語言能夠實現數據展現