The endpoint problem in flask

Source: Internet
Author: User

First a little bit about the flask code, time is limited, I slowly expand

The following is Add_url_rule code in flask source app.py.

It is mainly the correspondence between View_func-endpoint-url.

Flask, View_func and URL is not directly corresponding, is the URL first find endpoint, and then go through endpoint to find the corresponding View_func, a endpoint can only correspond to a view_func, in the register add_ Url_rule, if you do not specify endpoint, then endpoint will default to the function name, if the same endpoint in multiple URLs to register, there will be problems, see the code, will be judged before the corresponding to the present is not a, if not, Then throw an exception. It is certainly not possible to visit these URLs again. There is time to slowly expand this part of the content.

1 @setupmethod2     defAdd_url_rule (self, rule, endpoint=none, View_func=none, * *options):3         ifEndpoint isNone:4Endpoint =_endpoint_from_view_func (View_func)5options['Endpoint'] =Endpoint6 Methods7        ifMethods isNone:8Methods = GetAttr (View_func,'Methods', None)or('GET',)9         ifisinstance (Methods, string_types):Ten             RaiseTypeError ('allowed methods has to be iterables of strings,' One                             'For example: @app. Route (..., methods=["POST"])') AMethods = Set (Item.upper () forIteminchmethods) -  -Required_methods = Set (GetAttr (View_func,'Required_methods', ())) the  -Provide_automatic_options =getattr (View_func, -             'provide_automatic_options', None) -  +         ifProvide_automatic_options isNone: -             if 'OPTIONS'  not inchmethods: +Provide_automatic_options =True ARequired_methods.add ('OPTIONS') at             Else: -Provide_automatic_options =False -  -         #ADD the required methods now. -Methods |=Required_methods -  inRule = self.url_rule_class (rule, methods=methods, * *options) -Rule.provide_automatic_options =provide_automatic_options to  + self.url_map.add (rule) -         ifView_func is  notNone: theOld_func =Self.view_functions.get (Endpoint) *             ifOld_func is  notNone andOld_func! =View_func: $                 RaiseAssertionerror ('View function Mapping is overwriting an'Panax Notoginseng                                      'existing endpoint function:%s'%endpoint) -Self.view_functions[endpoint] = View_func

Endpoint issues in flask

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.