繁体   English   中英

内部服务器错误烧瓶-Python

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM