Django基础1
一,web框架的本质
web应用的本质就是一个socket的服务端。而用户的浏览器就是一个客户端,具体事例如下:
import socket sk = socket.socket() sk.bind(('127.0.0.1', 8090)) sk.listen() while 1: conn,addr=sk.accept() conn.recv(9000) conn.send(b'HTTP/1.1 200 ok\r\n\r\n')#必须写上这一句,才可以,只限英文,如果是中文还需要加上别的 conn.send(b'ok') conn.close()
对于真实开发中python web程序来说,一般分为两部分,服务器程序和应用程序
服务器程序负责对socket服务器进行封装,并在请求到来时,对请求的各种数据进行整理。
应用程序则负责具体的逻辑处理。为了方便应用程序的开发,就出现了众多的Web框架,例如:Django、Flask、web.py 等。不同的框架有不同的开发方式,但是无论如何,开发出的应用程序都要和服务器程序配合,才能为用户提供服务。
这样,服务器程序就需要为不同的框架提供不同的支持。这样混乱的局面无论对于服务器还是框架,都是不好的。对服务器来说,需要支持各种不同框架,对框架来说,只有支持它的服务器才能被开发出的应用使用。
这时候,标准化就变得尤为重要。我们可以设立一个标准,只要服务器程序支持这个标准,框架也支持这个标准,那么他们就可以配合使用。一旦标准确定,双方各自实现。这样,服务器可以支持更多支持标准的框架,框架也可以使用更多支持标准的服务器。
WSGI(Web Server Gateway Interface)就是一种规范,它定义了使用Python编写的web应用程序与web服务器程序之间的接口格式,实现web应用程序与web服务器程序间的解耦。
常用的WSGI服务器有uwsgi、Gunicorn。而Python标准库提供的独立WSGI服务器叫wsgiref,Django开发环境用的就是这个模块来做服务器。
import socket sk = socket.socket() sk.bind(('127.0.0.1', 8090)) sk.listen() while 1: conn,addr=sk.accept() info=conn.recv(9000).decode("utf-8") a=info.split("\r\n")[0] a1=a.split()[1] print(a1) if a1=="/index/": response=b'insex' else:response = b"404 not found!" conn.send(b'HTTP/1.1 200 ok\r\n\r\n')#必须写上这一句,才可以,只限英文,如果是中文还需要加上别的 conn.send(response) conn.close()
import socket sk = socket.socket() sk.bind(('127.0.0.1', 8090)) sk.listen() def index(a1): s="ghghghghghg不讲课" return bytes(s,encoding="utf-8") while 1: conn,addr=sk.accept() info=conn.recv(9000).decode("utf-8") a=info.split("\r\n")[0] a1=a.split()[1] print(a1) if a1=="/index/": response=index(a1) else:response = b"404 not found!" conn.send(b'HTTP/1.1 200 ok\r\nContent-Type: text/html; charset=utf-8\r\n\r\n')#必须写上这一句,才可以,只限英文,如果是中文还需要加上别的 conn.send(response) conn.close()
from wsgiref.simple_server import make_server def index(a1): s="ghghghghghg不讲课" return bytes(s,encoding="utf-8") def run(environ,start_response): start_response('200 ok', [('Content-Type', 'text/html; charset=utf-8'),]) a1=environ['PATH_INFO'] if a1: response=index(a1) else:response = b"404 not found!" return [response,] if __name__=='__main__': httpd=make_server('127.0.0.1',8090,run) httpd.serve_forever()
from wsgiref.simple_server import make_server import jinja2 def index(a1): with open("xiaoqiang.html", "r", encoding="utf8") as f: s = f.read() template = jinja2.Template(s) # 生成一个jinja2的Template(模板)对象 data = {"name": "ergou", "hobby_list": ["dd", "rr", "ee"]} response = template.render(data) # 本质上是完成了字符串的替换 return bytes(response, encoding="utf8") def run(environ,start_response): start_response('200 ok', [('Content-Type', 'text/html; charset=utf-8'),]) a1=environ['PATH_INFO'] if a1: response=index(a1) else:response = b"404 not found!" return [response,] if __name__=='__main__': httpd=make_server('127.0.0.1',8090,run) httpd.serve_forever()
from wsgiref.simple_server import make_server import jinja2 import pymysql def index(a1): with open("xiaoqiang.html", "r", encoding="utf8") as f: s = f.read() template = jinja2.Template(s) # 生成一个jinja2的Template(模板)对象 conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="123456", db="userinfo", charset="utf8") cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("SELECT name, hobby FROM info1") user = cursor.fetchone() cursor.close() conn.close() hobby_list = user["hobby"].split() user["hobby_list"] = hobby_list response = template.render(user) # 本质上是完成了字符串的替换 return bytes(response, encoding="utf8") def run(environ,start_response): start_response('200 ok', [('Content-Type', 'text/html; charset=utf-8'),]) a1=environ['PATH_INFO'] if a1: response=index(a1) else:response = b"404 not found!" return [response,] if __name__=='__main__': httpd=make_server('127.0.0.1',8090,run) httpd.serve_forever()
Django的静态文件配置
STATIC_URL = '/static/' # HTML中使用的静态文件夹前缀 STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), # 静态文件存放位置 ]
Django必备的三个模块
from django.shortcuts import HttpResponse, render, redirect
1,HttpResponse
内部传入一个字符串参数,返回给浏览器。
render
除request参数外还接受一个待渲染的模板文件和一个保存具体数据的字典参数。
将数据填充进模板文件,最后把结果返回给浏览器。
作用类似于jinja2
redirect
接受一个URL参数,表示跳转到指定的URL。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!