我试图用按钮点击按钮来调用一个函数。以下是我的代码片段,可以让我的问题更加清晰。
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'))
从文本字段生成名称,菜肴和位置。得到这个错误我做错了什么?
答案 0 :(得分:1)
我在堆栈overflow上看到了一些类似的问题。我想当你用
创建用户实例时User(username).add_restaurant(name, cuisine, location)
用户未正确实例化。试试:
else:
user = user(username)
user.add_restaurant(name, cuisine, location)
如果这有任何帮助,请告诉我们。