简体   繁体   English

发送一部分request.path到url_for Python / HTML

[英]send a part of request.path to url_for Python/HTML

my url is http://example.com/en/cat/ap+da+w_pl Now I have a-tag like this: 我的网址是http://example.com/en/cat/ap+da+w_pl现在我有一个这样的标签:

<a href="{{ url_for('category',
                    feature=request.path+"+"+att.get('u_sg'))}}">
                    {{ att.get('name') }}
                </a>

request.path is giving me ' /en/cat/ap+da+wh_pl ' BUT, I need only /ap+da+w_pl How to do it? request.path给了我' /en/cat/ap+da+wh_pl '但是,我只需要/ap+da+w_pl怎么做?

I need to pass only 'ap+da+w_pl' from out of request.path from HTML only, as I have to use it in pre-coded View of Flask and my view is like THIS: 我只需要从HTML的request.path中仅传递'ap + da + w_pl',因为我必须在预先编码的Flask视图中使用它,而我的视图就像这样:

@app.route('<lan_c>/cat/<string:feature>')
def category(feature, page):

You could split the result by / and get the last key: 您可以将结果除以/并得到最后一个键:

>>> r = 'http://example.com/en/cat/ap+da+w_pl'.split('/')
>>> r[-1]
'ap+da+w_pl'

This would work for /en/cat/ap+da+wh_pl the same way: 对于/en/cat/ap+da+wh_pl的工作方式相同:

>>> r = '/en/cat/ap+da+w_pl'.split('/')
>>> r[-1]
'ap+da+w_pl'

Prepend the / if needed: 如果需要,在/

>>> '/'+(r[-1])
'/ap+da+w_pl'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM