九龙坡区建设二校的网站,做网站用什么代码编写,购物网站的设计思路,asp网站搭建教程WEB框架的三大组件#xff1a;路由系统、控制器#xff08;含模板渲染#xff09;、数据库操作
微型框架#xff1a;依赖第三方写的socket#xff0c;WSGI#xff0c; 本身功能少
安装#xff1a; pip install bottle pip install flask 安装flask#xff0c;同时安…WEB框架的三大组件路由系统、控制器含模板渲染、数据库操作
微型框架依赖第三方写的socketWSGI 本身功能少
安装 pip install bottle pip install flask 安装flask同时安装了MarkupSafe、Werkzeug、Jinja2、itsdangerous。 Werkzeug是Python的WSGI规范的实用函数库。使用广泛基于BSD协议是Flask使用的底层WSGI库 itsdangerous是flask中引用的一个第三方包用来解决生成token等网络安全问题 MarkupSafe为 Python 实现 XML/HTML/XHTML 标记安全字符串 Jinja2是一个模板语言,是一个现代的、设计友好的、依照django模板的python模板语言
pip install tornado 已经安装过。
Bottle
一个程序文件完成整个网站
from bottle import template,Bottle
root Bottle()root.route(/hello/)
# 装饰器定义了URL即/hello/这个url由index这个函数来处理就是路由系统
def index():return Hello World!root.run(hostlocalhost, port8080)
# 这里就是启动webserver服务器然后等待请求
运行整个Python程序 浏览器端请求 上面的路由即装饰器是静态路由还可以使用动态路由
root.route(/wiki/pagename) def callback(pagename): ... pagename作为参数变量名匹配字符串。
root.route(/object/id:int) def callback(id): ... id是一个int型的参数变量名。
root.route(/show/name:re:[a-z]) def callback(name): ... name是一个正则表达式参数变量。
root.route(/static/path:path) def callback(path): return static_file(path, rootstatic) 定义路径类似Django中的静态文件路径主要是定义文件在服务器存储中的位置。 root指定的是项目中的一个目录这里指定了项目中的static在这个目录下有testcss.css文件可以访问 修改一下 如果root为static不变还可以这样访问 对于路由对应的函数除了返回字符串还可以使用模板
root.route(/hello/)
# 装饰器定义了URL即/hello/这个url由index这个函数来处理
def index():# return Hello World!return template(bHello {{name}}!/b,name小花)
对于template除了像上面直接在其中写模板外还可以直接指定一个模板文件如
return template(‘index.html’)
创建index.html文件。这里要注意的是默认bottle找模板的路径在bottle中有如下配置变量
TEMPLATE_PATH [./, ./views/]即默认模板查找顺序是先在项目根目录然后是views目录我们可以添加自己定义的目录最终程序
from bottle import template,Bottle,static_file
import bottle
bottle.TEMPLATE_PATH.append(./templates/)root Bottle()# 装饰器定义了URL即/hello/这个url由index这个函数来处理
root.route(/hello/)
def index():print(bottle.TEMPLATE_PATH)# return Hello World!# return template(bHello {{name}}!/b,name小花)return template(index.html)root.route(/sta/path:path)
def callback(path):return static_file(path,rootstatic)root.run(hostlocalhost, port8080)
运行程序后打印的TEMPLATE_PATH为[./, ./views/, ./templates/]但是却找不到模板文件 经过反复的测试查找资料这个问题我个人的理解是
这个路径是linux系统的路径在windows系统下失去效果windows系统下需要添加windows的绝对路径使用os.path.abspath(os.path.join(os.path.dirname(__file__), views)来获取windows下的绝对路径
from bottle import run,template,Bottle,TEMPLATE_PATH
import os
app Bottle()
TEMPLATE_PATH.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), templates)))
TEMPLATE_PATH.append(os.path.abspath(os.path.join(os.path.dirname(__file__), views)))
print(TEMPLATE_PATH)app.route(/)
app.route(/hello/name)
def index(namedefault):return template(hello.html,namename)run(app,hostlocalhost,port8080)
这时的TEMPLATE_PATH为
[D:\\website\\bottlepro\\templates, ./, ./views/, D:\\website\\bottlepro\\views]
这时再访问就没有问题了。 请求方法路由
root.route(/hello/, methodPOST) def index(): ... root.get(/hello/) def index(): ... root.post(/hello/) def index(): ... root.put(/hello/) def index(): ... root.delete(/hello/) def index(): ...
一个简单的登陆
from bottle import template,Bottle,static_file,TEMPLATE_PATH,request,redirect
import osTEMPLATE_PATH.append(os.path.abspath(os.path.join(os.path.dirname(__file__),templates)))
root Bottle()# 装饰器定义了URL即/hello/这个url由index这个函数来处理
root.route(/login/,method[GET,POST])
def login():if request.method GET:return template(login.html)else:# v request.forms # POST的数据都保存# v request.query # GET发来的请求数据# v request.body # POST发来的请求数据u request.forms.get(user)p request.forms.get(pwd)print(u,p)return redirect(/index/)
root.route(/index/)
def index():return template(index.html)root.route(/sta/path:path)
def callback(path):return static_file(path,rootstatic)root.run(hostlocalhost, port8080)
# 这里就是启动webserver服务器然后等待请求
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
bodyh1Bottle登录/h1form action/login/ methodpostinput typetext nameuser placeholder用户名input typepassword namepwd placeholder密码input typesubmit value提交/form
/body
/html
Bottle中的request其实是一个LocalReqeust对象其中封装了用户请求的相关信息
request.headers 请求头信息
request.query get请求信息
request.forms post请求信息
request.files 上传文件信息
request.params get和post请求信息
request.GET get请求信息
request.POST post和上传信息
request.cookies cookie信息
request.environ 环境相关相关
bottle的模板在使用for循环遍历列表生成ulli时的问题
root.route(/index/)
def index():user_list [{id: 1, name: root1, age: 18},{id: 2, name: root2, age: 19},{id: 3, name: root3, age: 20},{id: 4, name: root4, age: 21},]return template(index.html, user_listuser_list)
模板index.html
body{{user_list}}hr/ul% for item in user_list :li{{item}}li/% end/ul
/body
预想的是这样 实际是这样 多出来的这些空行都是li::marker/li为何会多产生这些标签怎么去掉希望高手指点一二。
Flask
基本框架与Bottle差不多如下
from flask import Flaskapp Flask(__name__)app.route(/index/)
def index():return hello world!if __name__ __main__:app.run()
启动后 默认是在5000端口。访问 在Flask中可以对静态文件和模板路径进行配置相关参数如下 默认模板路径就是templates 这个不像Bottle项目下创建了templates就可以直接找到。
传递参数
app.route(/index/)
def index():# return hello world!return render_template(flaskindex.html,k1hello,k2[a,b,c],k3{name:haha1,age:17})模板
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
bodyh1Flask index/h1{{k1}} !-- 单值 --{{k2}} !-- 列表 --hr/ul{% for item in k2 %}li{{item}}/li{% endfor %}/ul{{k3}} !-- 字典 --hr/{% for k,v in k3.items() %}{{k}}--{{v}}br{% endfor %}
/body
/html
还可以传递函数 模板中要在变量后加上小括号 Flask的动态路由方式
app.route(/user/username)app.route(/post/int:post_id)app.route(/post/float:post_id)app.route(/post/path:path)app.route(/login, methods[GET, POST])
DEFAULT_CONVERTERS { default: UnicodeConverter, string: UnicodeConverter, any: AnyConverter, path: PathConverter, int: IntegerConverter, float: FloatConverter, uuid: UUIDConverter, }
与bottle一样也有方法请求路由即在路由中增加“method”参数
对于Http请求Flask会讲请求信息封装在request中(werkzeug.wrappers.BaseRequest)提供的如下常用方法和字段以供使用
request.method request.args request.form request.values request.files request.cookies request.headers request.path request.full_path request.script_root request.url request.base_url request.url_root request.host_url request.host
Flask中使用cookie使用make_response函数包装render_template生成的对象有set_cookie方法。先导入make_response
app.route(/index/)
def index():# return hello world!# return render_template(flaskindex.html,k0myfun,k1hello,k2[a,b,c],k3{name:haha1,age:17})obj make_response(render_template(flaskindex.html,k0myfun,k1hello,k2[a,b,c],k3{name:haha1,age:17}))obj.set_cookie(usern,xiaohua)return obj
Flask中重定向redirect
url_for别名这里的别名就是函数名不需要在route中单独定义 Flask中使用session先导入sessionfrom flask import session
要使用session需要先配置一个SECRET_KEY在Flask对象上设置设置后才能在函数中使用session[key]value的方式
from flask import Flask,render_template,make_response,session,request,redirect,url_forapp Flask(__name__)
app.config.update(SECRET_KEYb_5#y2LF4Q8z\n\xec]/) # 要使用session必须设置这个参数def myfun():return a测试传递函数/aapp.route(/index/)
def index():# return hello world!# return render_template(flaskindex.html,k0myfun,k1hello,k2[a,b,c],k3{name:haha1,age:17})obj make_response(render_template(flaskindex.html,k0myfun,k1hello,k2[a,b,c],k3{name:haha1,age:17}))obj.set_cookie(usern,xiaohua)return objapp.route(/login/,methods[GET,POST])
def login():if request.method POST:session[user] request.form.get(user) # 设置session内容url url_for(redirectaliastest)return redirect(url)else:return render_template(login.html)app.route(/testurlfor)
def redirectaliastest():print(session) # login中设置的session在这里能打印出return render_template(flaskindex.html)if __name__ __main__:app.run()
关于Flask的中间件调用机制
所有的WSGI在执行时即实例化时要先执行__call__()方法
def __call__(self, environ: dict, start_response: t.Callable) - t.Any:return self.wsgi_app(environ, start_response)
这个方法返回的是是执行wsgi_app方法的结果然后在进入到Flask。
我们可以将这个wsgi_app换成我们自定义的类在__call__方法中添加我们自己的代码最后在调用原来的wsgi_app这样就在请求处理前先经过了我们自己的处理像Django的中间件就是这种机制实现的。
from flask import Flask,render_template,make_response,session,request,redirect,url_forapp Flask(__name__)
app.config.update(SECRET_KEYb_5#y2LF4Q8z\n\xec]/)def myfun():return a测试传递函数/aapp.route(/index/)
def index():# return hello world!# return render_template(flaskindex.html,k0myfun,k1hello,k2[a,b,c],k3{name:haha1,age:17})obj make_response(render_template(flaskindex.html,k0myfun,k1hello,k2[a,b,c],k3{name:haha1,age:17}))obj.set_cookie(usern,xiaohua)return objapp.route(/login/,methods[GET,POST])
def login():if request.method POST:session[user] request.form.get(user)url url_for(redirectaliastest)return redirect(url)else:return render_template(login.html)app.route(/testurlfor)
def redirectaliastest():print(session)return render_template(flaskindex.html)class Foo:def __init__(self,w):self.w wdef __call__(self, environ,start_response):print(自己的中间件)obj self.w(environ,start_response)return obj
if __name__ __main__:app.wsgi_app Foo(app.wsgi_app) # 将原来的wsgi_app换成我们的类Foo加上括号即Foo会先init然后call# call的时候先执行我们的逻辑相当于中间件最后执行原来的wsgi_app# 这种方法有点装饰器的设计模式app.run()
启动后页面请求过程 Flask的消息取一次就消失
放置在session中使用flash(value)取用get_flashed_message()取完就删除了。