小编Lua*_*yen的帖子

Flask - 我如何始终在url中包含语言代码

我开始使用Flask几周了,并且正在尝试将i18n和l10n应用到我的Flask应用程序中.这是我真正想要实现的行为:

用户输入website.com将被重定向到website.com/en/website.com/fr/取决于其设置中的Accept-Languages标头或默认语言.

这是我目前的实施:

# main_blueprint.py
mainBlueprint = Blueprint('main', __name__)

@mainBlueprint.route('/')
def index(lang):
    return "lang: %" % lang


# application.py
app.register_blueprint(mainBlueprint, url_defaults={'lang': 'en'})
app.register_blueprint(mainBlueprint, url_prefix='/<lang>')
Run Code Online (Sandbox Code Playgroud)

这样,当我打字website.com或者website.com/en/它会响应时website.com.除非我打字,website.com/fr否则会回复/fr/.但是,/en/即使它是默认选项,我也希望始终包含.


我在Flask doc中尝试了指南URL处理器模式,但是当我输入时website.com它响应了404 error.当我将其language_code包含在url中时它才能正常工作- 这不是我想要的行为.


先感谢您!

python internationalization flask

5
推荐指数
1
解决办法
983
查看次数

在Django过滤器中获取小写/无空格名称等于小写/无空格参数的对象

我想获得所有products在没有空间/小写版本name字段等于没有空间/小写版本input_product_name在Django参数。我怎样才能做到这一点?

伪代码

def get_or_add_product(input_product_name):
    return Product.objects.filter(where no-space/lowercase version of name == input_product_name.strip().lower())
Run Code Online (Sandbox Code Playgroud)

python regex django filter

1
推荐指数
1
解决办法
1041
查看次数

标签 统计

python ×2

django ×1

filter ×1

flask ×1

internationalization ×1

regex ×1