2015-12-16 24 views
5

Flask Blueprint cihazımın herhangi bir yolu çalıştırmadan önce her zaman bir yöntem çalıştırmasını istiyorum. Bunun yerine özel bir dekoratör ile benim planında her rota yöntemini dekorasyon, böyle bir şey yapabilmek istiyorum: Flask: Bir plandaki her rotadan önce nasıl bir yöntem çalıştırılır?

def my_method(): 
    do_stuff 

section = Blueprint('section', __name__) 

# Register my_method() as a setup method that runs before all routes 
section.custom_setup_method(my_method()) 

@section.route('/two') 
def route_one(): 
    do_stuff 

@section.route('/one') 
def route_two(): 
    do_stuff 

Sonra temelde hem /section/one ve /section/two route_one() veya route_two() kodu uygulamadan önce my_method() çalışacaktır.

Bunu yapmanın bir yolu var mı?

cevap

10

. Bunun gibi:

@section.before_request 
def my_method(): 
    do_stuff 

Bu, otomatik olarak işlevi, plana ait olan tüm yollardan önce çalışması için kaydeder.

İlgili konular