-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
42 lines (32 loc) · 1.2 KB
/
test.py
File metadata and controls
42 lines (32 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
db = SQLAlchemy(app)
class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), nullable=False)
author = db.Column(db.String(80), nullable=False)
publication_year = db.Column(db.Integer, nullable=False)
def __repr__(self):
return f"<Book {self.title}>"
@app.before_first_request
def create_table():
db.create_all()
@app.route('/')
def book():
book_list = Book.query.all()
return render_template('index.html', book_list=book_list)
@app.route('/add_book', methods=['GET', 'POST'])
def add_book():
if request.method == 'POST':
title = request.form['title']
author = request.form['author']
publication_year = request.form['publication_year']
new_book = Book(title=title, author=author, publication_year=publication_year)
db.session.add(new_book)
db.session.commit()
return redirect(url_for('books'))
return render_template('add_book.html')
if __name__ == '__main__':
app.run(debug=True)