| @ -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) | |||||
| @ -0,0 +1,14 @@ | |||||
| from flask_sqlalchemy import SQLAlchemy | |||||
| db = SQLAlchemy() | |||||
| class Song(db.Model): | |||||
| id = db.Column(db.Integer, primary_key=True) | |||||
| title = db.Column(db.String(100), nullable=False) | |||||
| author = db.Column(db.String(100), nullable=False) | |||||
| year = db.Column(db.Integer, nullable=False) | |||||
| album = db.Column(db.String(100), nullable=False) | |||||
| lyrics = db.Column(db.Text, nullable=False) | |||||
| def __repr__(self): | |||||
| return f'<Song {self.title}>' | |||||
| @ -0,0 +1,23 @@ | |||||
| {% extends 'base.html' %} | |||||
| {% block content %} | |||||
| <h2>Add New Song</h2> | |||||
| <form method="POST"> | |||||
| <label for="title">Title:</label> | |||||
| <input type="text" id="title" name="title" required> | |||||
| <label for="author">Author:</label> | |||||
| <input type="text" id="author" name="author" required> | |||||
| <label for="year">Year:</label> | |||||
| <input type="number" id="year" name="year" required> | |||||
| <label for="album">Album:</label> | |||||
| <input type="text" id="album" name="album" required> | |||||
| <label for="lyrics">Lyrics:</label> | |||||
| <textarea id="lyrics" name="lyrics" required></textarea> | |||||
| <button type="submit">Add Song</button> | |||||
| </form> | |||||
| {% endblock %} | |||||
| @ -0,0 +1,19 @@ | |||||
| <!DOCTYPE html> | |||||
| <html lang="en"> | |||||
| <head> | |||||
| <meta charset="UTF-8"> | |||||
| <title>My Song Lyrics</title> | |||||
| <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> | |||||
| </head> | |||||
| <body> | |||||
| <div class="container"> | |||||
| <h1>My Song Lyrics</h1> | |||||
| <nav> | |||||
| <a href="{{ url_for('index') }}">Home</a> | |||||
| <a href="{{ url_for('add_song') }}">Add Song</a> | |||||
| </nav> | |||||
| <hr> | |||||
| {% block content %}{% endblock %} | |||||
| </div> | |||||
| </body> | |||||
| </html> | |||||
| @ -0,0 +1,12 @@ | |||||
| {% extends 'base.html' %} | |||||
| {% block content %} | |||||
| <h2>Song List</h2> | |||||
| <ul> | |||||
| {% for song in songs %} | |||||
| <li> | |||||
| <a href="{{ url_for('song', song_id=song.id) }}">{{ song.title }} by {{ song.author }}</a> | |||||
| </li> | |||||
| {% endfor %} | |||||
| </ul> | |||||
| {% endblock %} | |||||
| @ -0,0 +1,9 @@ | |||||
| {% extends 'base.html' %} | |||||
| {% block content %} | |||||
| <h2>{{ song.title }}</h2> | |||||
| <p><strong>Author:</strong> {{ song.author }}</p> | |||||
| <p><strong>Year:</strong> {{ song.year }}</p> | |||||
| <p><strong>Album:</strong> {{ song.album }}</p> | |||||
| <pre>{{ song.lyrics }}</pre> | |||||
| {% endblock %} | |||||