我是Flask的新人。我试着写一个非常简单的应用程序。由于我来自MVC世界,我试图按照简约的MVC结构来组织我的代码。所以我得到了以下内容:
priceChecker/
core/
controllers/
__init__.py
ItemController.py
models/
model.py
item.py
__init__.py
services
__init__.py
static/
templates/
__init__.py
priceChecker.py
priceChecker.py:
import os
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from cassandra.cluster import Cluster
app = Flask(__name__)
# load config from this file, app.py
app.config.from_object(__name__)
#app.run(debug=True, use_debugger=False, use_reloader=False)
cluster = Cluster()
keyspace = 'testdb'
db = cluster.connect(keyspace)
from priceChecker.core.controllers.ItemController import ItemController
app.debug = True
@app.route('/search', methods=['GET', 'POST'])
def show_search_form():
item = ItemController()
if request.method == 'POST':
return 'post'
else:
a='get'
first_order = item.retrieveItems
for order in first_order:
print(order.shipname)
return render_template('search.html')
model.py
from priceChecker.priceChecker import db
class Model(db):
def __init__(self, tableName):
self.__tablename__ = tableName
def getAll(self, conditions=[]):
try:
result = db.execute("SELECT * FROM " + self.__tablename__)
return result
except ValueError:
print("Oops, something went wrong!...")
item.py
from priceChecker.core.models.model import Model as db
class Item(db):
def __init__(self):
super().__init__('orders')
def getAllItems(self):
return db.getAllItems()
ItemController.py
from priceChecker.core.models.item import Item
class ItemController:
def __init__(self):
self.item = Item()
def retrieveItems(self):
allItms = self.item.getAllItems()
return allItms
运行应用时,我遇到了一些错误:
属性错误:' str'对象没有属性' profile_manager'
如果我将以下行移到顶部
来自priceChecker.core.controllers.ItemController import ItemController
我正在
ImportError:无法导入名称' db'
建议?
修改
屏幕上显示错误:
builtins.ImportError
ImportError: cannot import name 'db'
Traceback (most recent call last)
File "/Users/bigweld/Desktop/priceChecker/priceChecker/priceChecker.py", line 5, in <module>
from priceChecker.core.controllers.ItemController import ItemController
File "/Users/bigweld/Desktop/priceChecker/priceChecker/core/controllers/ItemController.py", line 1, in <module>
from priceChecker.core.models.item import Item
File "/Users/bigweld/Desktop/priceChecker/priceChecker/core/models/item.py", line 1, in <module>
Open an interactive python shell in this framefrom priceChecker.core.models.model import Model as db
File "/Users/bigweld/Desktop/priceChecker/priceChecker/core/models/model.py", line 2, in <module>
from priceChecker.priceChecker import db
ImportError: cannot import name 'db'
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
答案 0 :(得分:1)
想出来!这个问题与我扩展课程的方式有关。
priceChecker.py和ItemController.py很好。
现在,让我们从model.py开始,我有:
from priceChecker.priceChecker import db
class Model(db):
def __init__(self, tableName):
self.__tablename__ = tableName
def getAll(self, conditions=[]):
try:
result = db.execute("SELECT * FROM " + self.__tablename__)
return result
except ValueError:
print("Oops, something went wrong!...")
应该是什么时候:
from priceChecker.priceChecker import db
class Model():
def __init__(self, tableName):
self.__tablename__ = tableName
def getAll(self, conditions=[]):
try:
result = db.execute("SELECT * FROM " + self.__tablename__)
return result
except ValueError:
print("Oops, something went wrong!...")
我想这是失败的,因为我试图从db扩展Model(),这是一个属性而不是类。
现在,item.py应该是:
from priceChecker.core.models.model import Model
class Item(Model):
def __init__(self):
print('\n\nInitializing Item model...')
super().__init__('orders')
def getAllItems(self):
return Model.getAll(self)
此时一切正常,这最初是问题所在,但正如您所看到的,我使用的是“模型”这个词,因为我无法导入为“数据库”。作为我最初的陈述。