带选项的Flask add_url_rule会引发意外的关键字参数 [英] Flask add_url_rule with options throws unexpected keyword argument

查看:594
本文介绍了带选项的Flask add_url_rule会引发意外的关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Flask Blueprints来添加一个动态的运行时间路由。我打电话给 add_url_rule()并将一些数据传递给视图。

  def add_special_route(route_url,data):
myblueprint.add_url_rule(route_url,view_func = my_special_view_function,specialparameter = data)

def my_special_view_function(specialparameter):
return specialparameter.some_string_x

编译器不喜欢specialparameter。错误是意外关键字参数。我期待它进入add_url_rule的** options kwargs。

我查看了使用**选项的示例的文档。没有太多。



我在做什么错?

解决方案

默认视图的参数应该在 defaults 关键字参数的字典中传递:

  def add_special_route(route_url,data):
myblueprint.add_url_rule(
route_url,view_func = my_special_view_function,
defaults = {'specialparameter':data})

请参阅 路由注册文档


I'm trying to use Flask Blueprints to add a dynamic run time route. I'm calling add_url_rule() and passing some data to the view.

def add_special_route(route_url, data):
    myblueprint.add_url_rule(route_url, view_func=my_special_view_function, specialparameter=data)

def my_special_view_function(specialparameter):
    return specialparameter.some_string_x

The compiler doesn't like specialparameter. The error is unexpected keyword argument. I was expecting it to go into the **options kwargs on add_url_rule.

I've looked at the docs for examples that use **options. Not much.

What am I doing wrong?

解决方案

Default parameters for a view should be passed in in a dictionary with the defaults keyword argument:

def add_special_route(route_url, data):
    myblueprint.add_url_rule(
        route_url, view_func=my_special_view_function,
        defaults={'specialparameter': data})

See the URL Route Registrations documentation.

这篇关于带选项的Flask add_url_rule会引发意外的关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆