How to route non-ascii URLs in Flask python

此生再无相见时 提交于 2021-02-16 22:46:53

问题


Good afternoon, everyone!

I have a problem with the routing my URL adress to Flask, precisely with running it in web-browser. All I want is to transfer the sharp symbol "#" and some Russian words (as like "#привет" or "#ПомогитеМнеПожалуйста") together.

The screenshot of error:

My programming code at the moment looks like this:

# -*- coding: utf-8 -*-
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/hashtags/' + b'<names>'.decode('utf-8'), methods=['GET'])
def get_hashtags(names):
    return jsonify({'Segmentation Hashtags': names})


if __name__ == '__main__':
    app.run(port=9876)

So, basically, <names> is a parameter from function get_hashtag that is used for transfering my future hashtag to the web-browser using jsonify. I need to find the way of transfering any hashtag I want with sharp symbol "#" plus Russian letters. As far as I know, there is an ASCII-coding methods, but I have no idea how to use it properly. And <names> have structure: "#привет"

Thanks in advance!


回答1:


You have to use %23 instead of #, because the hash symbol marks a fragment in a URL. Wikipedia

So the actual URL app.route is getting is /hashtag/

It seems to be impossible to get the content after #. See this.




回答2:


Try to decode your url before passing it to your view methods, this way:

@app.route('/hashtags/<names>'.encode('utf-8'), methods=['GET'])


来源:https://stackoverflow.com/questions/49847076/how-to-route-non-ascii-urls-in-flask-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!