web.py框架 web.seeother VS web.redirect

       在网站开发中,会有一些页面跳转功能需求,好比登陆后跳转到用户的我的页面,好比退出登陆后跳转到登陆页面。python

       web.py中,使用web.seeother或者web.redriect就能够实现这个功能。好比web

import web

urls = (
  '/hello', 'Index',
  '/welcome','Welcome'
)

app = web.application(urls, globals())

render = web.template.render('templates/')

class Welcome(object):
	def GET(self):
		return render.welcome();

class Index(object):
	def GET(self):
		return render.hello_form()

	def POST(self):
		raise web.seeother('/welcome')
		#raise web.redirect('/welcome')

if __name__ == "__main__":
    app.run()



 

web.py的页面跳转有两种实现方式:web.seeotherweb.redirect。通常状况下建议使用web.seeother,而不是web.redirect,这两种方式有什么区别呢?浏览器

web.seeother返回的status code303 See Other缓存

wKiom1TUU1fDlggbAACrv921OfU882.jpg


web.redirect返回的status code301 Moved Permanently网络

wKioL1TUVEXgZWbxAACZIvED3Xg517.jpg

那么303 SeeOther301 MovedPermanently有什么区别呢?app

 

这两个状态码都是告诉客户端:你请求的资源移到了别处,我给你资源的地址,你去那个地方找吧。不一样的是:301是永久移动,303是暂时移动。ide

 

因为咱们用的是浏览器,并体会不到这个过程。若是作网络爬虫开发,这些状态码就很重要了。对于状态码返回301的网页,在记录地址映射时,直接记录新的Location便可,这样就避免了一次没必要要的请求,提升了请求效率。网站

 

对于服务端开发,应该多使用web.seeother,由于浏览器会缓存网页。若是使用web.redirect,浏览器会忽略缓存,直接访问网站。对于用户体验而言,是很差的。url

 

固然,有一种场景适合用web.redirect,即一般用户在浏览器中会收藏一些网页。为了使用户的收藏不失效,就要用web.redirectspa

相关文章
相关标签/搜索