Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

python - Flask: proper way of using routes and functions

I wrote a small flask server. However, one of the routes was getting large so i thought of creating an external .py file, that contains all the logic in a function, and when the route gets executed, it simply calls that function in that different python file.

However, i also had to get parameters from a form. So my new route was:

@app.route('/convert', methods=['POST'])
conversion_item = request.form.get('item')
conversion_method = request.form.get('method')
#HERE CALL THE FUNCTION
convertMy(conversion_item, conversion_method)
return redirect(url_for('index'))

However, when i execute this, i get:

  File "server.py", line 24
    conversion_item = request.form.get('item')
    ^
SyntaxError: invalid syntax

Do i have to put everything in that route inside a function? Then i will have a function under that route, that will call another function? That seems weird to me.

How can i "glue" this?

question from:https://stackoverflow.com/questions/66046845/flask-proper-way-of-using-routes-and-functions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the @ in @app.route represents a function decorator, it has to decorate a function. It is perfectly normal for a function to call another function.

@app.route('/convert', methods=['POST'])
def convert():
    conversion_item = request.form.get('item')
    conversion_method = request.form.get('method')
    #HERE CALL THE FUNCTION
    convertMy(conversion_item, conversion_method)
    return redirect(url_for('index'))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...