preface
Over the past few years, I have been struggling in the it industry. Along the way, many have summarized some high-frequency interviews in the python industry. I see most of the new blood in the industry, and I still have all kinds of difficult questions for the answers to all kinds of interview questions or collection
Therefore, I developed an interview dictionary myself, hoping to help everyone, and also hope that more Python newcomers can really join the industry, so that Python fire does not just stay in advertising.
Wechat applet search: Python interview dictionary
Or follow the original personal blog: https://lienze.tech
You can also focus on WeChat official account and send all kinds of interesting technical articles at random: Python programming learning.
route
Modern web applications use meaningful URL s, which helps users remember. Web pages will be more favored by users and improve the return rate
Use the route() decorator to bind functions to URL s
@app.route('/path/<int:uid>',methods=["GET"],endpoint="name")
Dynamic routing
By marking part of the URL as < variable_ Name > you can add variables to the URL. The marked part is passed to the view function as a keyword parameter as a parameter of the view function
By using converter:variable_name, you can optionally add a converter to specify rules for variables, similar to the routing parameters of Django
@app.route('/test/<username>') def test(username): return 'url: %s' % username
converter
Converter type | explain |
---|---|
int | positive integer |
float | Accept positive floating point numbers |
path | Similar to string, but can contain slashes |
uuid | Accept UUID string |
string | Strings other than slashes (default) |
Route naming
The flash routing system supports the same route naming method as django. The common writing method can be completed by using endpoint in the @ app.route decorator
Note: if the endpoint parameter is not set, the route is named after the function name by default
from flask import redirect @app.route('/test', endpoint="test") def test(): # Reverse parsing and redirection return redirect(url_for("home")) @app.route('/home', endpoint="home") def home(): return "home"
**url_for * * is used to resolve route names and generate corresponding routes
If the resolved route name has parameters, as shown below, it can be displayed in the URL_ After the for parameter, use the variable length dictionary to pass the dynamic parameters of the connection
from flask import redirect @app.route('/test', endpoint="test") def test(): # Reverse parsing and redirection return redirect(url_for("home",id=1)) # look here @app.route('/home/<id>', endpoint="home") def home(): return "home"
Request control
You can also control how the view can be accessed by defining the methods parameter on the route decorator
@app.route('/path/<int:uid>',methods=["GET","POST"],endpoint="views") def views(uid): if request.method == "GET": ... if request.method == "POST": ...
Routing essence
The source code of the app.route decorator method is as follows
def route(self, rule, **options): def decorator(f): endpoint = options.pop("endpoint", None) self.add_url_rule(rule, endpoint, f, **options) return f return decorator
The closure method of the decorator called add_url_rule method is used for routing mapping of views, so after a view is written, you can not only use the decorator, but also directly use this method for routing mapping definition
def views(): ... app.add_url_rule("/path","name",views, methods=["GET", "POST"])
add_url_rule(rule, endpoint, view_func, methods=["GET", "POST"], defaults = {'k': 'v'}, strict_slashes=True,), redirect_to="https://baidu.com" ''' rule: URL rule view_func: View function name endpoint: Route naming methods: How requests are allowed strict_slashes: The slash at the end of the route checks for strictness defaults: Default value, When URL There are no parameters in the function. When the function needs parameters, use defaults = {'k': 'v'} redirect_to: Direct redirection strict_slashes: This parameter is used to set whether our route is in strict mode, False Non strict mode, True Strict, the default is strict '''