Python flask peewee在web服务器上不工作
我无法从python脚本访问peewee数据库,除了编写数据的脚本(但奇怪的是在交互式shell中)。我正在用python、flask和peewee开发一个数据挖掘应用程序。我将在这里把它分解成一个字节大小的问题,但它的范围比我现在介绍的要大。所有python文件都位于同一文件夹中 基本过程是一个python命令行操作,它从纽约时报和instagram获取一些信息,并将它们存储到Peewee(sqllite)数据库中。然后,我使用一个flask应用程序来探索数据 下面是database.py中的一个模型:Python flask peewee在web服务器上不工作,python,database,sqlite,flask,peewee,Python,Database,Sqlite,Flask,Peewee,我无法从python脚本访问peewee数据库,除了编写数据的脚本(但奇怪的是在交互式shell中)。我正在用python、flask和peewee开发一个数据挖掘应用程序。我将在这里把它分解成一个字节大小的问题,但它的范围比我现在介绍的要大。所有python文件都位于同一文件夹中 基本过程是一个python命令行操作,它从纽约时报和instagram获取一些信息,并将它们存储到Peewee(sqllite)数据库中。然后,我使用一个flask应用程序来探索数据 下面是database.py中的
from peewee import *
class Story(Model):
title = CharField()
story = TextField()
date = DateField()
class Meta:
database = SqliteDatabase("newsalmost.db",threadlocals = True)
新闻几乎是这样的:
from database import *
class NewsAlmost(object):
def __init__(self):
self.db = SqliteDatabase("newsalmost.db",threadlocals = True)
story = Story.create(title = self.feed.stories[key]["title"], story = self.feed.stories[key],date = datetime.datetime.now(), is_relative = True)
如果是这样的话:
from database import *
class NewsAlmost(object):
def __init__(self):
self.db = SqliteDatabase("newsalmost.db",threadlocals = True)
story = Story.create(title = self.feed.stories[key]["title"], story = self.feed.stories[key],date = datetime.datetime.now(), is_relative = True)
然后我可以运行:
"python newslamost.py -g"
它将收集资料并将其写入数据库
然后我有一个名为webapp.py的文件,它是一个烧瓶应用程序
import newsalmost
from flask import Flask
from flask import send_file
import subprocess
app = Flask(__name__)
import os
import json
from database import *
@app.route("/")
def index():
r = []
for i in Image.select():
r.append(str(i))
return json.dumps(r)
“pythonwebapp.py”
我试图把它归结为核心问题。flask应用程序从未在数据库中看到任何内容。。永远
我知道它正确地记录了它们,因为我实际上可以在那个文件夹中运行“python”,导入数据库,然后从stories.select()中获得许多故事
更奇怪的是,我最初是以一种更理想的方式设计它的,flask应用程序只创建NewsMesst实例的一个新实例,然后调用该实例上的函数从数据库返回内容,这就成功了。。在开发模式下。但是,当我将它部署到我的web派系服务器(并让那里的一切都运行起来)时,我再次收到来自数据库的空响应。这是我试图直接引用flask中的数据库的尝试,我认为这可能是新闻,几乎是把事情搞砸了。。但是没有
我只是感到困惑,为什么sqllite数据库会在本地完全按照预期执行,但不是部署到Web服务器,而是。。。为什么我提供的flask代码没有从数据库中获取任何信息,而是在交互式shell工作中运行相同的数据库查询
有什么想法吗?我不知道这是否能解决您的问题,但您应该在两个模块中使用相同的数据库对象:
database.py
:
from peewee import *
db = SqliteDatabase("newsalmost.db",threadlocals = True)
class Story(Model):
title = CharField()
story = TextField()
date = DateField()
class Meta:
database = db
from database import *
class NewsAlmost(object):
def __init__(self):
self.db = db
newslest.py
:
from peewee import *
db = SqliteDatabase("newsalmost.db",threadlocals = True)
class Story(Model):
title = CharField()
story = TextField()
date = DateField()
class Meta:
database = db
from database import *
class NewsAlmost(object):
def __init__(self):
self.db = db
唉。。我想这是没有希望的。。。回到MongoDBi最终解决了这个问题。。。我刚在生产中使用了烧瓶测试模式。它明确地说永远不要做,但我对此没有任何问题。。。所以嘿这也是我的想法:这是一个有趣的概念,刚刚测试过:)