在dreamhost上的sqlite通过flask / JSON - 获得404s

时间:2014-02-23 03:44:30

标签: python json sqlite flask dreamhost

我有一个烧瓶应用程序,它查询sqlite数据库并通过jsonify将列表返回给html。它在localhost上运行正常,但我已经上传到dreamhost并且正在为jsonified列表获取404。我无法判断烧瓶与我的sqlite数据库,烧瓶或json通信是否存在问题。

这是烧瓶应用程序:

#!/usr/bin/python

import sqlite3 as sqlite
import json
from flask import Flask, g, jsonify, make_response, render_template

DEBUG = True
DATABASE = './whdt_combined.db'

#setup flask application
app = Flask(__name__)
app.config.from_object(__name__)

##########################
### DATABASE STUFF #######
##########################

#connect to the database
def connect_db():
    rv = sqlite.connect(app.config['DATABASE'])
    rv.row_factory = dict_factory
    return rv

#function for making rows nice
def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

#open new db connection if ones hasn't been opened
def get_db():
    if not hasattr(g, 'sqlite_db'):
        g.sqlite_db = connect_db()
    return g.sqlite_db

#when application stops, close db connection
@app.teardown_appcontext
def close_db(error):
    if hasattr(g, 'sqlite_db'):
        g.sqlite_db.close()

#easy function to nicely query the db
def query_db(query, args=(), one    =False):
    cur  = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv

##########################
### APPLICATION ##########
##########################

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

@app.route('/<data1>/<data2>/<region>/<year>/1/')
def datafunction1(data1, data2, region, year):
data = []
for row in query_db('SELECT ccode, country, year, region, ' + data1 + ' FROM whdt WHERE year = ' + year + ' AND region = "' + region + '" AND ' + data1 + ' != "" AND ' + data1 + ' > 0 AND ' + data1 + ' < 100 ORDER BY ' + data1 + ' DESC'):
    countrywhole = {
        'country' :row['country'],      
        'ccode':row['ccode'],
        'year':row['year'],
        'region':row['region'],
        data1:row[data1]
    }
    data.append(countrywhole)
return jsonify( { 'data': data } )    

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

这是我的文件夹设置:

/home/user/mydomain.com/myapp/whdt_combined.db
/home/user/mydomain.com/myapp/myapp.py
/home/user/mydomain.com/myapp/__init__.py
/home/user/mydomain.com/myapp/templates/index.html
/home/user/mydomain.com/myapp/static/a couple of css and js files

Flask渲染index.html时遇到问题。部分问题是我不知道如何通过dreamhost进行调试。我对这些东西还比较陌生,所以如果我遗漏了一些显而易见的东西,或者显然不知道我在说什么,我很抱歉。如果我在这里提供其他重要信息,请告诉我。并提前感谢您的帮助!

0 个答案:

没有答案