[英]Class instance has no attribute, error when calling a function in Flask
我試圖使用flask在按鈕單擊上調用函數。 這是我的代碼段,以使我的問題更清楚。
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'))
名稱,美食和位置均從文本字段生成。 我要怎么做才能得到這個錯誤?
由於在堆棧溢出上有一些類似的問題,我對此進行了研究。 我認為當您使用創建用戶實例時
User(username).add_restaurant(name, cuisine, location)
用戶未正確實例化。 嘗試:
else:
user = user(username)
user.add_restaurant(name, cuisine, location)
讓我們知道這是否有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.