flask custom converters and regular routing

Custom converters
acting converters: limiting the url address
custom converter Syntax: <converter: variable_name>
a custom converter: 
1. Custom BaseConverter converter must inherit the class, custom converter requires override the init method of the parent class, focusing attention parameters. 
2. When invoked, "re (". ") :. File_name", will pass as a parameter * args, then pass regex parent class as a regular, matching url, content matching view passed as a parameter file_name function. 
3. The custom converter requires registration, Re converter is called a Key
<ignore_js_op>
About to_python wherein the method
is first to achieve a simple routing switches to match three numbers, as follows:
<ignore_js_op>
on this basis we should be rewritten three methods BaseConverter inside:
<ignore_js_op>
after viewing the source code, we can find digital NumberConverter any length matching. 
NumberConverter is IntegerConverter parent. The method of to_python inside NumberConverter completed character string converted to digital conversion process means.

Before view function, you will first enter into the inside of the converter to_python. After a regular match before the view function call, we will enter facilitate the next step of the process to_python. The results of the first match, if we want to do further processing, can be written in to_python this convenient function inside its further processing.
By breakpoint debugging, we found that we turn out is not an int, but a py2 string type. 
<ignore_js_op>

so further refinement is to rewrite to_python method, adding: 
return value is the value we do regular match.
<ignore_js_op>
this time: 
<ignore_js_op>
we do not have to rewrite to_python, look at our actual needs ......

About to_url of these methods
if sometimes we do not get the expected results, we can try to clean up the cookie cache ... 
we return to a simple model, as follows: 
<ignore_js_op>
<ignore_js_op>
by breakpoint debugging we found that our system only and does not enter into the to_python to_url. This is because to_url flask is used to redirect the time.
to_url it is called before starting a match, the collected order_id pass in the first time processing, which proceeds as in the basic BaseConverter string code. It is for the convenience to_python processing. It matches with the success it does not matter. We view the source code found in to_url to find NumberConverter digits were processed dissatisfaction with the specified number of bits make up 0.
In short, after starting the match, before a successful match, matching order_id out to do the first step in treatment is to ensure that you can successfully enter to_python inside. The ultimate success or not, will be called regardless of the final redirect, playing before the war ... the purpose is to let the value unlimited match regex

Use the built-int converter
<ignore_js_op>

The final version of
<ignore_js_op>
<ignore_js_op>
(8) Regular Routes
in web development, may appear to limit user access rules scenes, so this time we need a regular match. Limit access, access optimization.
Import Converter Pack
<ignore_js_op>

custom converters and implement
custom converters
<ignore_js_op>
comes with several converters
<ignore_js_op>

More technical information may concern: gzitcast

Guess you like

Origin www.cnblogs.com/heimaguangzhou/p/11689905.html