如果这个Q没有意义,我很抱歉,但是有没有一种方法可以在URL中(仅)使用path参数构建它时用连字符替换空格
我的设想如下:
我有一个查看方法,如下所示:
from app.service import *
@app.route('/myapp/<search_url>',methods=['GET','POST'])
def search_func(search_url):
print(search_url) // This prints 'hi-martin king' and i need to pass 'hi-martin king' to below with preserving space
search_q = search(search_url)
return render_template('wordsearch.html',data=search_q['data'])
search_url
我从模板传递我有search
函数,它将search_url
作为参数(def search(search_url): .....
),并执行我从上面的服务导入的所有操作(for ref)
现在,当我运行时,示例URL为
....myapp/hi-martin%20king
这里我保留了在数据库中执行查询的空间(在db中它存储为martin king
),但我不想在URL中显示相同的内容,而是用连字符替换它
我有其他方法来更改数据库中的所有值(删除空格,但这不是一个合适的解决方案)
....myapp/hi-martin-king (or with an underscore) ....myapp/hi-martin_king
在这里,我如何保留空间,将其作为参数传递给函数,同时只在URL中替换?这可能吗
非常感谢您的帮助
如果要在保留空格的同时查询数据库,只需使用
urllib.parse.unquote
对其进行url卸载,并在url中保留转义的空格,如下所示:如果需要原始版本,则需要取消引用中带有
%
的字符串现在有了无引号的字符串,可以用连字符或下划线替换空格
相关问题 更多 >
编程相关推荐