Import WTForm class to update content

Viewed 9

I've got this class in folder module > Book.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///new-books-collection.db"
db = SQLAlchemy(app)

class Book(db.Model):
    __tablename__ = "Books"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(250), unique=True, nullable=False)
    author = db.Column(db.String(250), nullable=False)
    rating = db.Column(db.String(2), nullable=False)

and i want to import in main.py

from module.Book import Book

I made my CRUD and everythings works except for the update. This is the function

@app.route("/edit", methods=["GET", "POST"])
def edit():
    if request.method == "POST":
        book_id = 1
        book = Book.query.get(book_id)
        book.title = request.form['title']
        book.author = request.form['author']
        book.rating = request.form['rating']
        print(book.rating) # print the updated rating
        db.session.commit() # but commit seems doesn't work
        return redirect(url_for('home'))

    book_id = request.args.get('id')
    book = Book.query.get(book_id)
    
    return render_template("edit_add.html", book=book)

Can some one figure it out?

Can some one figure it out?

0 Answers
Related