Xem bên dưới, đây là một số mã (mà tôi đã cắt bớt) from the Flask app object. Mã này handle thêm một quy tắc url (được cũng là những gì được gọi bằng bình khi bạn làm app.route() trên quan điểm của bạn) ....
@setupmethod
def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
""" I remove a ton the documentation here.... """
if endpoint is None:
endpoint = _endpoint_from_view_func(view_func)
options['endpoint'] = endpoint
methods = options.pop('methods', None)
# if the methods are not given and the view_func object knows its
# methods we can use that instead. If neither exists, we go with
# a tuple of only `GET` as default.
if methods is None:
methods = getattr(view_func, 'methods', None) or ('GET',)
methods = set(methods)
# ... SNIP a bunch more code...
rule = self.url_rule_class(rule, methods=methods, **options)
rule.provide_automatic_options = provide_automatic_options
self.url_map.add(rule)
Như bạn thấy, Flask sẽ làm điều đó là để darndest đảm bảo rằng các phương pháp được xác định rõ ràng. Bây giờ, Flask dựa trên Werkzeug và dòng ...
rule = self.url_rule_class(rule, methods=methods, **options)
... thường sử dụng Werkzeug's Rule lớp. Lớp này có tài liệu sau cho đối số "phương thức" ...
Một chuỗi các phương pháp http áp dụng quy tắc này. Nếu không được chỉ định, tất cả các phương thức đều được cho phép.
Vì vậy, điều này nói với tôi rằng bạn có thể làm điều gì đó như sau ...
from werkzeug.routing import Rule
app = Flask(__name__)
def my_rule_wrapper(rule, **kwargs):
kwargs['methods'] = None
return Rule(rule, **kwargs)
app.url_rule_class = my_rule_wrapper
tôi đã không kiểm tra này ra, nhưng hy vọng có thể giúp bạn đi đúng hướng .
Edit:
Hoặc bạn chỉ có thể sử dụng câu trả lời DazWorrall, mà dường như tốt hơn: P
Thay đổi url_rule_class cũng sẽ hoạt động tốt :) Nó có thể sạch hơn nếu bạn muốn mở nhiều (hoặc tất cả) tuyến đường lên đến bất kỳ phương pháp nào. – DazWorrall