【发布时间】:2015-12-13 07:25:31
【问题描述】:
我正在尝试使用烧瓶在按钮单击时调用函数。这是我的代码的 sn-p,可以让我的问题更清楚。
class User:
def __init__(self, username):
self.username = username
def find(self):
user = graph.find_one("User", "username", self.username)
return user
def add_restaurant(self, name, cuisine, location):
user=self.find()
restaurant = Node("Restaurant", id=str(uuid.uuid4()),name=name)
graph.create(restaurant)
graph.create(rel(user,"LIKES", restaurant))
rest_type = Node("Cuisine", cuisine=cuisine)
graph.create(rest_type)
graph.create(rel(restaurant,"SERVES", rest_type))
loc = Node("Location", location=location)
graph.create(loc)
graph.create(rel(restaurant, "IN", loc))
add_restaurant 函数应该在 Neo4j 图数据库中创建 3 个节点及其对应关系,它工作正常。
但是,当我从我的 Flask 文件中调用这个函数时,我得到一个
AttributeError: User instance has no attribute 'add_restaurant'
这是Flask文件中的函数
@app.route('/add_restaurant', methods=['POST'])
def add_restaurant():
name = request.form['name1']
cuisine = request.form['cuisine1']
location = request.form['location1']
username = session.get('username')
if not name or not cuisine or not location:
if not name:
flash('You must enter a restaurant name')
if not cuisine:
flash('What cuisine does the restaurant belong to')
if not location:
flash('You must enter a location')
else:
User(username).add_restaurant(name, cuisine, location) #Error comes from here.
return redirect(url_for('index'))
姓名、美食和位置是从文本字段生成的。我做错了什么来得到这个错误?
【问题讨论】:
-
尝试将打印添加到您的 init 函数并确保
User是您认为的类 -
您是否从定义的地方导入了 User 类?
-
@NicoleWhite 你好,妮可!看到你的回复真是一个惊喜:)。我实际上是在使用你的 neo4j-flask 教程作为我练习的基础,但我正在调整你的代码以在我自己的数据库上运行,因为我认为这会让学习变得更有趣(希望你不介意)。你的回复让我很开心。是的,我正确导入了 User 类。尊重!
-
当然!这就是我建造它的原因。 :) 我建议你按照@JoranBeasley 所说的在 init 函数中打印一些内容。
标签: python flask neo4j flask-restful