Skip to main content
 首页 » 编程设计

python之Python 中的字符串操作。解析网址

2025年05月04日24wuhuacong

print full_url = request.get_host()+request.get_full_path() 

结果:

127.0.0.1:8000/test/en/

或

127.0.0.1:8000/test/ru/

如何判断字符串结尾是/en/还是/ru/

请您参考如下方法:

使用endswith:

full_url.endswith('/en/') or full_url.endswith('/ru/') 

如果你发现有更多的扩展需要覆盖,你可以考虑使用any:

any(s.endswith(ext) for ext in ['/ru/', '/en/'])