圖標的插件css
fontawsome:html
後臺管理:python
easyuijquery
jqueryuiweb
不少網站都會用:sql
bootstrap :引入jQuery:(2.x,1.1.)數據庫
:引入jsdjango
bootstrap模板:bootstrap
bootstrap後臺管理模板:瀏覽器
bxslider:輪播圖
jquerylazyload:延遲加載,全部的圖片延遲加載
http://www.cnblogs.com/wupeiqi/articles/5813161.html
faulure_limit:2 若是遇到兩個圖片是abslouat, button:0 距離最底部。延遲加載就不作了。
瀏覽網站的本質;
運行socket_server:
衆所周知,對於全部的Web應用,本質上其實就是一個socket服務端,用戶的瀏覽器其實就是一個socket客戶端。
#!/usr/bin/env python #coding:utf-8 import socket def handle_request(client): buf = client.recv(1024) client.send("HTTP/1.1 200 OK\r\n\r\n") client.send("Hello, Seven") def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost',8000)) sock.listen(5) while True: connection, address = sock.accept() handle_request(connection) connection.close() if __name__ == '__main__': main()
上述經過socket來實現了其本質,而對於真實開發中的python web程序來講,通常會分爲兩部分:服務器程序和應用程序。服務器程序負責對socket服務器進行封裝,並在請求到來時,對請求的各類數據進行整理。應用程序則負責具體的邏輯處理。爲了方便應用程序的開發,就出現了衆多的Web框架,例如:Django、Flask、web.py 等。不一樣的框架有不一樣的開發方式,可是不管如何,開發出的應用程序都要和服務器程序配合,才能爲用戶提供服務。這樣,服務器程序就須要爲不一樣的框架提供不一樣的支持。這樣混亂的局面不管對於服務器仍是框架,都是很差的。對服務器來講,須要支持各類不一樣框架,對框架來講,只有支持它的服務器才能被開發出的應用使用。這時候,標準化就變得尤其重要。咱們能夠設立一個標準,只要服務器程序支持這個標準,框架也支持這個標準,那麼他們就能夠配合使用。一旦標準肯定,雙方各自實現。這樣,服務器能夠支持更多支持標準的框架,框架也可使用更多支持標準的服務器。
WSGI(Web Server Gateway Interface)是一種規範,它定義了使用python編寫的web app與web server之間接口格式,實現web app與web server間的解耦。
python標準庫提供的獨立WSGI服務器稱爲wsgiref。
python中的web框架:2類:
wsgi:
本身開發一個web框架:
基於wsgi,或者本身寫socket:
!/usr/bin/env python #coding:utf-8 from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return '<h1>Hello, web!</h1>' if __name__ == '__main__': httpd = make_server('', 8000, RunServer) //只要請求來就執行RunServer函數 print "Serving HTTP on port 8000..." httpd.serve_forever() 自定義Web框架
#!/usr/bin/env python3 # Created by han on 2016/9/1 from wsgiref.simple_server import make_server def f1(): return f1 def f2(): return f2 def RunServer(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) #要實現想用用戶的不一樣請求 #environ 這個裏面封裝這用戶的全部信息 #下面這種根據用戶不一樣的訪問返回不一樣的內容,在jdango中叫路由系統 if environ.url == 'index': return f1() elif environ.url == 'login': return f2() else: return 404 if __name__ == '__main__': httpd = make_server('', 8000, RunServer) print("Serving HTTP on port 8000...") httpd.serve_forever()
web框架指演變二:
#!/usr/bin/env python #coding:utf-8 from wsgiref.simple_server import make_server def index(): return 'index' def login(): return 'login'
#路由匹配
def routers():
urlpatterns = ( ('/index/',index), ('/login/',login), ) return urlpatterns def RunServer(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) url = environ['PATH_INFO'] #獲取到用戶的請求 urlpatterns = routers() func = None for item in urlpatterns: if item[0] == url: func = item[1] break if func: return func() else: return '404 not found' if __name__ == '__main__': httpd = make_server('', 8000, RunServer) print "Serving HTTP on port 8000..." httpd.serve_forever()
二、模板引擎
在上一步驟中,對於全部的login、index均返回給用戶瀏覽器一個簡單的字符串,在現實的Web請求中通常會返回一個複雜的符合HTML規則的字符串,因此咱們通常將要返回給用戶的HTML寫在指定文件中,而後再返回。如:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <h1>Index</h1> </body> </html>
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <form> <input type="text" /> <input type="text" /> <input type="submit" /> </form> </body> </html>
#!/usr/bin/env python # -*- coding:utf-8 -*- from wsgiref.simple_server import make_server def index(): # return 'index' f = open('index.html') data = f.read() return data def login(): # return 'login' f = open('login.html') data = f.read() return data def routers(): urlpatterns = ( ('/index/', index), ('/login/', login), ) return urlpatterns def run_server(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) url = environ['PATH_INFO'] urlpatterns = routers() func = None for item in urlpatterns: if item[0] == url: func = item[1] break if func: return func() else: return '404 not found' if __name__ == '__main__': httpd = make_server('', 8000, run_server) print "Serving HTTP on port 8000..." httpd.serve_forever()
對於上述代碼,雖然能夠返回給用戶HTML的內容以現實複雜的頁面,可是仍是存在問題:如何給用戶返回動態內容?
MVC:
c:controllers -- 處理用戶請求
v:views -- 頁面模板
m : modal -- 數據庫的操做
MTV:
m:數據庫操做
t :template 頁面模板
v:views -- 處理用戶請求
Django:
1、建立django程序:
方法一、命令行
安裝jdango:pip3 install jdango
安裝後會有一個jango-admin.exe,把這個加到環境變量中
建立
django-admin startproject mysite(程序名) -- 執行後jdango程序就建立成功
方法2:pycharm
一、建立項目:
二、選擇位置及python版本
三、目錄建立成功
my_test_django:
my_test_django/:下面是配置文件
setting.py: #django的配置文件
urls.py: #請求的路由系統
wsgi.py: #wsgi socket
manage.py 是django的啓動文件
二、建立app
在一個preject下能夠建立多個app,多個app共享一個project:
如:一個project是一個運維繫統,下面有多個功能,如監控,cmdb。。。 這些每個功能就是一個app
操做
python manage.py startapp cmdb
apps:當前app的配置文件
tests:單元測試,測試某個功能
admin:配置後臺管理
3:寫路由和處理用戶請求
my_tests_django/urls:路由映射
from django.conf.urls import url from django.contrib import admin from cmdb import views urlpatterns = [ # url(r'^admin/', admin.site.urls), #這裏是默認的處理,先不用 url(r'^index/',views.index) #用戶訪問的url是^index/ 的就給路由到views.index的這個函數裏處理 ]
cmdb/views.py:處理用戶請求
from django.shortcuts import render from django.shortcuts import HttpResponse #倒入給用戶返回字符串的方法 # Create your views here.處理用戶請求 #django定義好了request是獲取用戶請求的關鍵字 def index(request): #django規定返回字符串須要封裝到一個對象中才能夠 # return '123' #這樣return,用戶是看不到的 return HttpResponse('hello world') #這樣用戶才能看到返回的內容
四、啓動jdango程序
方法1:命令行
python3 manage.py runserver 127.0.0.1:8000
方法2:pycharm中直接啓動
from django.shortcuts import render #jdango提供了一個專爲返回網頁的方法 from django.shortcuts import HttpResponse #倒入給用戶返回字符串的方法 # Create your views here.處理用戶請求 #django定義好了request是獲取用戶請求的關鍵字 def index(request): #django規定返回字符串須要封裝到一個對象中才能夠 #方法1: 本身打開網頁返回給用戶 # with open('my_test_django/templates/index.html') as html_file: # index = html_file.read() # return HttpResponse(index)#這樣用戶才能看到返回的內容 #方法2:使用render方法 print(request) return render(request,'index.html')
配置文件:
css,js以及插件圖片這些靜態文件存放位置:
本身建立一個靜態文件的目錄,並把js拷貝到這裏。
五、提交數據獲取數據
a、(內存版本:)
cmdb/views.py,處理用戶請求
from django.shortcuts import render #jdango提供了一個專爲返回網頁的方法 from django.shortcuts import HttpResponse #django給用戶返回字符串的方法
from django.shortcuts import redirect #跳轉功能 # Create your views here.處理用戶請求 #django定義好了request是獲取用戶請求的關鍵字 def index(request): #判斷用戶是不是post請求 if request.method == "POST": #獲取post請求裏面的值, user = request.POST.get('user',None) #這些值都是在index.html中定義的 email = request.POST.get('email',None) #None是默認值,若是沒有email的key返回爲None print(user,email)
return redirect('/home/') #這樣就跳轉到home的頁面
return render(request,'index.html') #正常請求就會返回這個默認的頁面
這個錯誤是跨站請求僞造,須要把post請求驗證給註釋掉。
實例:獲取和提交數據:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>shouye</title> </head> <body> <h1>提交數據:</h1> <form action="/index/" method="post"> <input type="text" name="user"/> <input type="email" name="email"/> <input type="submit" value="提交"/> </form> <h1>展現數據:</h1> <table border="1"> {% for item in data %} <tr> <td>{{ item.user }}</td> <td>{{ item.email }}</td> </tr> {% endfor %} </table> <script src="/statics/jquery-3.1.0.min.js"></script> </body> </html>
from django.shortcuts import render #jdango提供了一個專爲返回網頁的方法 from django.shortcuts import HttpResponse #django給用戶返回字符串的方法 # Create your views here.處理用戶請求 #django定義好了request是獲取用戶請求的關鍵字 USER_INPUT = [] def index(request): #判斷用戶是不是post請求 if request.method == "POST": #獲取post請求裏面的值, user = request.POST.get('user',None) #這些值都是在index.html中定義的 email = request.POST.get('email',None) temp = {'user':user,'email':email} USER_INPUT.append(temp) return render(request,'index.html', {'data':USER_INPUT}) #正常請求就會返回這個默認的頁面,而且把USER_INPUT傳遞給index.html
模板引擎:會把模板裏面的代碼渲染:替換成python的代碼。拿到一個已經替換完成的字符串
<table border="1"> {% for item in data %} <tr> <td>{{ item.user }}</td> <td>{{ item.email }}</td> </tr> {% endfor %} </table>
b、(數據庫版本:)
django:本身提供一個ORM,django的默認數據庫sqllate,也是在setting中配置
一、models:定義數據庫
二、setting:註冊app
三、建立數據庫:
執行命令,python manage.py makemigrations
python manage.py migrate
這樣就建立了表 UserInfo的表:
四、插入數據
定義數據庫表的對象
from django.db import models # Create your models here. class UserInfo(models.Model): #sqllata必須繼承models.Model #定義數據內容 user = models.CharField(max_length=32) #建立了字符串類型 email = models.CharField(max_length=32)
而後在views.py中操做數據庫
models.UserInfo.objects.create(user=u,email=e) #操做modules的UserInfo表插入數據 ret_list = models.UserInfo.objects.all() #把數據庫中的內容所有取出來
models.UserInfo.objects.,filter(user='alex').content() 拿到用戶名==alex的對象的個數
from django.shortcuts import render #jdango提供了一個專爲返回網頁的方法
from django.shortcuts import HttpResponse #django給用戶返回字符串的方法
from cmdb import models
# Create your views here.處理用戶請求
#django定義好了request是獲取用戶請求的關鍵字
def index(request):
#判斷用戶是不是post請求
if request.method == "POST": #獲取post請求裏面的值,
u = request.POST.get('user',None) #這些值都是在index.html中定義的
e = request.POST.get('email',None)
models.UserInfo.objects.create(user=u,email=e) #操做modules的UserInfo表插入數據
ret_list = models.UserInfo.objects.all() #把數據庫中的內容所有取出來
#ret_list = 一行的對象 [UserInfo對象,UserInfo對象。。。。] 把ret_list傳遞給html在html中循環這個列表
return render(request,'index.html', {'data':ret_list}) #正常請求就會返回這個默認的頁面,而且把USER_INPUT傳遞給index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>shouye</title> </head> <body> <h1>提交數據:</h1> <form action="/index/" method="post"> <input type="text" name="user"/> <input type="email" name="email"/> <input type="submit" value="提交"/> </form> <h1>展現數據:</h1> <table border="1"> <tr> <th>用戶名</th> <th>郵箱</th> </tr> {% for line in data %} {# 循環views傳入的data,每一行是line#} <tr> <td>{{ line.user }}</td> {#line.user:獲取每一行對象的user#} <td>{{ line.email }}</td> {#line.user:獲取每一行對象的email#} </tr> {% endfor %} </table> <script src="/statics/jquery-3.1.0.min.js"></script> </body> </html>
django book