Before the bottle did a lot of introduction, but also wrote some articles to explain the shortcomings of bottle, recently found that in fact, some places said that not fair, so take this opportunity to correct.
Bottle is supported for syntax similar to Flask url_for, the specific use method is described below
Bottle Request.query such as the default is the STR type, there is a reason, such as I do agent for Google, the code is not necessarily utf8, if the forced conversion UTF8 will be an error
Previous bugs have also been fixed, such as the Mount ('/x ', app), which/x/and/x can access
OK, now formally into the topic, let's introduce some of the advanced uses of bottle
I. Smart URL creation
This part of the bottle document is not introduced (in fact, bottle clearly realized a lot of intimate features, do not know why not written on the document).
In the bottle class, there is a member function:
def get_url (self, Routename, **kargs): "" " Return a string, matches a named route" "" ScriptName = request.env Iron.get (' Script_name ', '). Strip ('/') + '/' Location = Self.router.build (Routename, **kargs). Lstrip ('/') Return Urljoin (Urljoin ('/', scriptname), location) def get_url (self, Routename, **kargs): "" " return a string that mat Ches a named route "" " ScriptName = Request.environ.get (' script_name ', '). Strip ('/') + '/' location = Self.route R.build (Routename, **kargs). Lstrip ('/') return Urljoin (Urljoin ('/', scriptname), location)
So where does this routename come from? See the parameters of the route adorner:
def route (self, path=none, method= ' GET ', Callback=none, Name=none, Apply=none, Skip=none, **config): def route (self , Path=none, method= ' GET ', Callback=none, Name=none, Apply=none, Skip=none, **config):
The name parameter is Routename (it must be said here, this is better than flask, you need to specify name, but not to implement URL_FOR, the entire framework is very complex)
So see here Everyone also understand, bottle's URL generator is bound on the bottle instance, so cross-instance access is not done by default.
and possibly due to the bottle of the micro, so the source of the default bottle is deliberately wrapped out a function:
For name in ' ' route get post put delete error mount hook install uninstall '. Split (): globals () [Name] = Make_def Ault_app_wrapper (name) URL = make_default_app_wrapper (' Get_url ') del name for name in ' ' route get post put delete error MoU NT Hook install uninstall '. Split (): globals () [Name] = make_default_app_wrapper (name) URL = Make_default_app _wrapper (' Get_url ') del name
The advantage of this is that if the project uses only the default bottle instance, the URL can be used directly in the template, instead of having to pass the bottle instance in again.
Correction, bottle Get_url can not be called across the app, such as the Mount app calls the main app Get_url will be wrong, because at this time the Script_name is the path of the current page, so the Assembly will be messy, so do not try.
But how do you get the template to access the local variables? We'll introduce
Two. Assigning a default variable to a template
Because I use the most is jinja2, so the template related to the introduction of JINJA2 as an example.
Because many instances of bottle are proxy patterns used, such as request,response,local, we can safely pass these variables into the template default variables.
The code is also simple:
From bottle import BaseTemplateBaseTemplate.defaults.update (dict ( request=request, local=local, )) From bottle import basetemplate BaseTemplate.defaults.update (Dict ( request=request, local=local, ))
If you are interested, you can also go directly to the source code, very understood
Three. Add filters to the template
Or take jinja2 as an example, give the code directly as follows:
From bottle import Basetemplateif ' filters ' not in basetemplate.settings: basetemplate.settings[' filters '] = {} filters = basetemplate.settings[' Filters ']def urlencode_filter (params): ' urlencode ' from Urllib Import UrlEncode return UrlEncode (params) filters.update (Dict ( urlencode=urlencode_filter )) From bottle import basetemplate If ' filters ' isn't in basetemplate.settings: basetemplate.settings[' filters '] = {} filt ers = basetemplate.settings[' filters ') def urlencode_filter (params): ' urlencode ' from Urllib Import UrlEncode return UrlEncode (params) filters.update (Dict ( urlencode=urlencode_filter ) )
OK, that's all, here, based on the bottle version is 0.10.9, if there is a place that does not match, please check the bottle version.