获取纯文本而不是Python脚本执行

时间:2015-05-18 03:08:34

标签: python nginx virtualenv uwsgi

我正试图在页面加载时使用jquery执行这个小请求:

$.ajax({
    type: "GET",
    url: "/static/query.py",
    cache: false,
    success: function (data) {
        $(body).text(data);
    }
});

在运行nginx的服务器上,python,在virtualenv中,使用Flask框架,返回的是query.py源代码,而不是从数据库中检索的数据。

query.py被标记为可执行文件,脚本有shebang:

#!/home/gabriel/project/bin

指向我的virtualenv中的bin。我认为有了基础,但问题仍然存在。

提示?

更多信息:

Flask脚本:

from flask import Flask, render_template

application = Flask(__name__)

@application.route('/')
def init():
    return render_template('index.html')

if __name__ == "__main__":
    application.run(host='0.0.0.0', debug=True)

uwsgi.py加载Flask脚本:

from myapp import application

if __name__ == "__main__":
    application.run()

query.py:

from db import * //Database model, SQLAlchemy.

def something():
    data = Kingdom.query.all()
    return data

something()

1 个答案:

答案 0 :(得分:0)

你应该运行在flask中的query.py中的实际代码。做类似的事情:

@application.route("/query"):
def get_data():
    .. whatever code you need; currently in query.py, you would probably use sqlalchemy/ flask-sqlalchemy
    data =  your data (dictionary? list?) 
    return jsonify(data=data) # flask will turn your data into a proper JSON string to send back as a response to the ajax call.

不要忘记从烧瓶中导入jsonify,请参阅文档here。 还可以在上面的示例中将“/static/query.py”重命名为“/ query”,或者您认为合适的任何其他内容。您还可以通过AJAX调用发送flask参数,以便在查询中处理;例如,过滤参数。将问题集中在进一步的指导上。