![](/img/trans.png)
[英]MySQL with Python Flask in Openshift: Internal Server Error
[英]internal server error Flask - Python
這是我在Python-Flask中的代碼。 在這里,我要輸入劇院(劇院表)的數據,然后獲取相應的ID,然后將屏幕添加到“屏幕”表中的相應theatre_id。
問題是Theatre被添加到數據庫中,並且我能夠獲取ID。 Screen的代碼似乎不起作用(如果我注釋掉session.screen add和commit語句,for循環也可以工作)並且如果沒有發生屏幕提交,即使回滾也不會發生。
session_theatre = Session_theatre()
session_screen = Session_screen()
id = 1
if request.method == "POST":
if form.validate_on_submit():
name = str(form.name.data)
city = str(form.location.data)
address = str(form.address.data)
no_of_screen = int(form.total_no_screen.data)
if (name !="" and name!=" " and city != "" and city != " " and address != ""and address != " " and no_of_screen != None):
t = Theatre(name,city,address,1)
try:
session_theatre.add(t)
session_theatre.commit()
query = session_theatre.query(Theatre).filter_by(name=name,city =city).all()
for i in query :
id = i
for i in range (0,no_of_screen):
flash(id)
screen = Screen(str(i+1),1,20,1,20,id)
session_screen.add(screen)
session_screen.commit()
flash("Successfully added !!")
except :
session_screen.rollback()
session_theatre.rollback()
flash("Oops something went wrong !!")
finally:
session_screen.close()
session_theatre.close()
else :
flash("Please fill the input")
return render_template('admin/add_theatre.html',form = form)
屏幕型號
class Screen(db.Model):
__tablename__ = "screens"
id = db.Column(db.Integer,primary_key=True)
screen_num = db.Column(db.String(1))
seat_row_start = db.Column(db.Integer)
seat_row_end = db.Column(db.Integer)
seat_col_start = db.Column(db.Integer)
seat_col_end = db.Column(db.Integer)
theatre_id = db.Column(db.Integer, db.ForeignKey('theatres.id'))
def __init__(self, screen_num,
seat_row_start,seat_row_end,seat_col_start,seat_col_end,theatre_id):
self.screen_num = screen_num
self.seat_row_start = seat_row_start
self.seat_row_end = seat_row_end
self.seat_col_start = seat_col_start
self.seat_col_end = seat_col_end
self.theatre_id = theatre_id
要從session_theatre
的查詢中獲取列表,必須在末尾添加all()
:
query = session_theatre.query(Theatre).filter_by(name=name,city =city).all()
該查詢返回Theatre
對象列表,您可以訪問遍歷列表的每個Theatre
對象的id
屬性,並通過其名稱訪問該屬性:
for obj in query :
for i in range (0,no_of_screen):
flash(obj.id)
screen = Screen(i+1,1,20,1,20,obj.id)
session_screen.add(screen)
session_screen.commit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.