我编写了一个在终端中运行的 python 脚本,并使用 Flask 将其移植到网络上。我已经完成了教程的部分内容(具体来说:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
)
我正在为将我在 Python 脚本中使用的所有函数放置在何处而苦苦挣扎。作者()使用此代码进行基本视图:
def index():
user = {'nickname': 'Miguel'} # fake user
posts = [ # fake array of posts
{
'author': {'nickname': 'John'},
'body': 'Beautiful day in Portland!'
},
{
'author': {'nickname': 'Susan'},
'body': 'The Avengers movie was so cool!'
}
]
return render_template("index.html",
title='Home',
user=user,
posts=posts)
问题是我没有一个函数可以调用。我有 15 个左右,看起来 Flask 只允许我为每个视图调用一个函数。所以我不太确定将我的“主”函数将调用的所有辅助函数放在哪里。
以作者的示例代码为例。如果我有一个函数getPosts()
返回一个 post 对象数组,我应该把它放在哪里?
即使我被允许将其放在路线的主要功能下(我认为无论如何都是不允许的),这样做似乎是一个糟糕的组织。
Edit:
这是我的views.py 文件:
1 from flask import Flask
2 app = Flask(__name__)
3 from flask import render_template
4 from app import app
5 from app import helpfulFunctions
6
7 def testFunction():
8 return 5;
9
10 @app.route('/')
11 @app.route('/index')
12 def index():
13 #allPlayers = processGender(mainURL, menTeams)
14 myNum = testFunction()
15 return render_template('index.html', title = 'Home', user = user)