|
|
|
@ -0,0 +1,45 @@ |
|
|
|
from flask import Flask, render_template, request, redirect, url_for |
|
|
|
from models import db, Song |
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///songs.db' |
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
|
|
|
db.init_app(app) |
|
|
|
|
|
|
|
@app.before_request |
|
|
|
def before_request(): |
|
|
|
if not hasattr(app, 'db_initialized'): |
|
|
|
db.create_all() |
|
|
|
app.db_initialized = True |
|
|
|
|
|
|
|
@app.route('/') |
|
|
|
def index(): |
|
|
|
songs = Song.query.all() |
|
|
|
return render_template('index.html', songs=songs) |
|
|
|
|
|
|
|
@app.route('/song/<int:song_id>') |
|
|
|
def song(song_id): |
|
|
|
song = Song.query.get_or_404(song_id) |
|
|
|
return render_template('song.html', song=song) |
|
|
|
|
|
|
|
@app.route('/add', methods=['GET', 'POST']) |
|
|
|
def add_song(): |
|
|
|
if request.method == 'POST': |
|
|
|
title = request.form['title'] |
|
|
|
author = request.form['author'] |
|
|
|
year = request.form['year'] |
|
|
|
album = request.form['album'] |
|
|
|
lyrics = request.form['lyrics'] |
|
|
|
|
|
|
|
new_song = Song(title=title, author=author, year=year, album=album, lyrics=lyrics) |
|
|
|
db.session.add(new_song) |
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
return redirect(url_for('index')) |
|
|
|
|
|
|
|
return render_template('add_song.html') |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
with app.app_context(): |
|
|
|
db.create_all() |
|
|
|
app.run(debug=True) |