-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
49 lines (38 loc) · 1.19 KB
/
app.py
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
43
44
45
46
47
48
49
from flask import Flask, render_template, request, redirect, url_for
from database import db
# Creating Flask App
app = Flask(__name__)
# Database Name
db_name = 'vehicle.db'
# Configuring SQLite Database URI
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_name
# To suppress warnings while making modifications
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Initialising SQLAlchemy with Flask App
db.init_app(app)
""" Creating Database with App Context"""
def create_db():
with app.app_context():
db.create_all()
""" Creating Routes """
@app.route("/")
def home():
details = Vehicle.query.all()
return render_template("home.html", details=details)
@app.route("/add-vehicle", methods=['GET', 'POST'])
def add_vehicle():
if request.method == 'POST':
v_name = request.form.get('vehicle')
price = request.form.get('price')
add_detail = Vehicle(
name=v_name,
price=price
)
db.session.add(add_detail)
db.session.commit()
return redirect(url_for('home'))
return render_template("vehicle.html")
if __name__ == "__main__":
from models import Vehicle
create_db()
app.run(debug=True)