41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
from flask import Flask, render_template, request, redirect, url_for
|
|
from models import db, Item
|
|
from datetime import datetime as dt
|
|
|
|
app = Flask(__name__)
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///items.db'
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
db.init_app(app)
|
|
|
|
@app.before_request
|
|
def initialize_app():
|
|
if not hasattr(app, 'is_initialized'):
|
|
db.create_all()
|
|
app.is_initialized = True
|
|
|
|
|
|
@app.route('/')
|
|
def home():
|
|
items = Item.query.all()
|
|
testList = [1,2,3,"Apple"]
|
|
username = "Test_Username"
|
|
return render_template('home.html', items=items, testList=testList, username=username)
|
|
|
|
@app.route('/add', methods=['GET', 'POST'])
|
|
def add_item():
|
|
if request.method == 'POST':
|
|
name = request.form['name']
|
|
description = request.form['description']
|
|
new_item = Item(name=name, description=description)
|
|
db.session.add(new_item)
|
|
db.session.commit()
|
|
return redirect(url_for('home'))
|
|
return render_template('add_item.html')
|
|
|
|
@app.route('/about')
|
|
def about():
|
|
return render_template('about.html', now=dt.now())
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|