Django(一)
HTTP协议对于收发消息的格式要求如下图:
wsgiref版返回动态的HTML文件
1 ''' 2 根据访问的不同路径返回不同的内容 3 实现不同的用户得到不同的HTML页面 4 ''' 5 6 import time 7 from wsgiref.simple_server import make_server 8 9 10 def home(url): 11 s = 'this is {} page'.format(url) 12 return bytes(s,encoding='utf8') 13 14 def index(url): 15 return b'<h1>index page</h1>' 16 17 def user(url): 18 c_time = str(time.time()) 19 with open('user.html','r') as f: 20 data_s = f.read() 21 data_s = data_s.replace('@@xx@@',c_time) #用时间戳模拟不同的用户访问 22 return bytes(data_s,encoding='utf8') 23 24 def login(url): 25 with open('login.html','rb') as f: 26 return f.read() 27 28 url_func = [ 29 ('/index/',index), 30 ('/home/',home), 31 ('/user/',user), 32 ('/login/',login), 33 ] 34 35 def run_server(environ,start_response): 36 ''' 37 按照wsgiref的要求定义一个run_server函数 38 :param environ: 跟请求相关的参数 39 :param start_response: 40 :return: 41 ''' 42 start_response('200 OK',[('Content-Type','text/html;charset=utf8'),]) 43 # print('查看environ参数:',environ) #查看environ参数 44 url = environ['PATH_INFO'] #拿到用户输入的url 45 print(url) 46 47 for i in url_func: 48 if url == i[0]: 49 func = i[1] 50 break 51 else: 52 func = None 53 54 if func: 55 msg = func(url) 56 else: 57 msg = b'<h1>404</h1>' 58 return [msg,] 59 60 if __name__ == '__main__': 61 httpd = make_server('127.0.0.1',8000,run_server) 62 print(httpd) 63 httpd.serve_forever()
jinja2返回动态的HTML文件
1 ''' 2 利用jinja2进行路径切割替换 3 ''' 4 5 from wsgiref.simple_server import make_server 6 from jinja2 import Template 7 import pymysql 8 9 10 def home(url): 11 s = 'this is {} page'.format(url) 12 return bytes(s,encoding='utf8') 13 14 def index(url): 15 return b'<h1>index page</h1>' 16 17 def user(url): 18 conn = pymysql.connect( 19 host = '127.0.0.1', 20 port = 3306, 21 user = 'root', 22 password = '123456789', 23 database = 'day61', 24 charset = 'utf8', 25 ) 26 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 27 cursor.execute('select * from user') 28 ret = cursor.fetchall() 29 #在页面上显示出来 30 with open('user.html','r',encoding='utf8') as f: 31 data_s = f.read() 32 template = Template(data_s) 33 msg = template.render({'user_list':ret}) 34 return bytes(msg,encoding='utf8') 35 36 37 def login(url): 38 with open('login1.html','rb') as f: 39 return f.read() 40 41 url_func = [ 42 ('/index/',index), 43 ('/home/',home), 44 ('/user/',user), 45 ('/login/',login), 46 ] 47 48 def run_server(environ,start_response): 49 ''' 50 按照wsgiref的要求定义一个run_server函数 51 :param environ: 跟请求相关的参数 52 :param start_response: 53 :return: 54 ''' 55 start_response('200 OK',[('Content-Type','text/html;charset=utf8'),]) 56 # print('查看environ参数:',environ) #查看environ参数 57 url = environ['PATH_INFO'] #拿到用户输入的url 58 # print(url) 59 60 for i in url_func: 61 if url == i[0]: 62 func = i[1] 63 break 64 else: 65 func = None 66 67 if func: 68 msg = func(url) 69 else: 70 msg = b'<h1>404</h1>' 71 return [msg,] 72 73 if __name__ == '__main__': 74 httpd = make_server('127.0.0.1',8000,run_server) 75 # print(httpd) 76 httpd.serve_forever()
Django命令行创建方式:
django-admin startproject mysite
Django基础重要的3个要素
from django.shortcuts import HttpResponse, render, redirect