问题
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