简体   繁体   中英

internal server error Flask - Python

This is my code in Python - Flask. Here I am entering data for a Theatre (Theatre table) and then fetching the respective id and then adding the screen to the respective theatre_id in the Screen table.

The problem is Theatre gets added to the database and I am able to fetch the id. The code for Screen doesn't seem to work (for loop does work if I comment out the session.screen add and commit statement) And even the rollback doesn't happen if screen commit doesn't happen.

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)

Screen Model

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

To get a list from the query for session_theatre you have to add all() at the end:

query = session_theatre.query(Theatre).filter_by(name=name,city =city).all()

The query returns a list of Theatre objects and you can access the id attribute of each Theatre object iterating over the list and accessing the attribute by its name:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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