Permalink
Browse files

Create, edit and delete item by form

  • Loading branch information...
greyli committed Dec 30, 2018
1 parent cfc08fa commit f7f73554e504fd7b3e8845ecb038ef41bbb5cbff
Showing with 110 additions and 3 deletions.
  1. +47 −2 app.py
  2. +41 −0 static/style.css
  3. +3 −1 templates/base.html
  4. +10 −0 templates/edit.html
  5. +9 −0 templates/index.html
49 app.py
@@ -3,7 +3,7 @@
import sys

import click
from flask import Flask, render_template
from flask import Flask, render_template, request, url_for, redirect, flash
from flask_sqlalchemy import SQLAlchemy

# SQLite URI compatible
@@ -15,6 +15,7 @@


app = Flask(__name__)
app.config['SECRET_KEY'] = 'dev'
app.config['SQLALCHEMY_DATABASE_URI'] = prefix + os.path.join(app.root_path, 'data.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

@@ -82,7 +83,51 @@ def page_not_found(e):
return render_template('404.html'), 404


@app.route('/')
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
title = request.form['title']
year = request.form['year']

if not title or not year or len(year) > 4:
flash('Invalid input.')
return redirect(url_for('index'))

movie = Movie(title=title, year=year)
db.session.add(movie)
db.session.commit()
flash('Item Created!')
return redirect(url_for('index'))

movies = Movie.query.all()
return render_template('index.html', movies=movies)


@app.route('/movie/edit/<int:movie_id>', methods=['GET', 'POST'])
def edit(movie_id):
movie = Movie.query.get_or_404(movie_id)

if request.method == 'POST':
title = request.form['title']
year = request.form['year']

if not title or not year or len(year) > 4:
flash('Invalid input.')
return redirect(url_for('edit', movie_id=movie_id))

movie.title = title
movie.year = year
db.session.commit()
flash('Item Updated.')
return redirect(url_for('index'))

return render_template('edit.html', movie=movie)


@app.route('/movie/delete/<int:movie_id>', methods=['POST'])
def delete(movie_id):
movie = Movie.query.get_or_404(movie_id)
db.session.delete(movie)
db.session.commit()
flash('Item Deleted.')
return redirect(url_for('index'))
@@ -33,6 +33,43 @@ nav li a:hover {
background-color: #111;
}

.alert {
position: relative;
padding: 7px;
margin: 7px 0;
border: 1px solid transparent;
color: #004085;
background-color: #cce5ff;
border-color: #b8daff;
border-radius: 5px;
}

input[type=text] {
border: 1px solid #ddd;
}

input[name=year] {
width: 50px;
}

.btn {
font-size: 12px;
padding: 3px 5px;
text-decoration: none;
cursor: pointer;
background-color: white;
color: black;
border: 1px solid #555555;
border-radius: 5px;
}

.btn:hover {
text-decoration: none;
background-color: black;
color: white;
border: 1px solid black;
}

.movie-list {
list-style-type: none;
padding: 0;
@@ -57,6 +94,10 @@ nav li a:hover {
float: right;
}

.inline-form {
display: inline;
}

.imdb {
font-size: 12px;
font-weight: bold;
@@ -10,7 +10,9 @@
{% endblock %}
</head>
<body>

{% for message in get_flashed_messages() %}
<div class="alert">{{ message }}</div>
{% endfor %}
<h2>
<img alt="Avatar" class="avatar" src="{{ url_for('static', filename='images/avatar.png') }}">
{{ user.name }}'s Watchlist
@@ -0,0 +1,10 @@
{% extends 'base.html' %}

{% block content %}
<h3>Edit item</h3>
<form method="post">
Name <input type="text" name="title" autocomplete="off" required value="{{ movie.title }}">
Year <input type="text" name="year" autocomplete="off" required value="{{ movie.year }}">
<input class="btn" type="submit" name="submit" value="Update">
</form>
{% endblock %}
@@ -2,10 +2,19 @@

{% block content %}
<p>{{ movies|length }} Titles</p>
<form method="post">
Name <input type="text" name="title" autocomplete="off" required>
Year <input type="text" name="year" autocomplete="off" required>
<input class="btn" type="submit" name="submit" value="Add">
</form>
<ul class="movie-list">
{% for movie in movies %}
<li>{{ movie.title }} - {{ movie.year }}
<span class="float-right">
<a class="btn" href="{{ url_for('edit', movie_id=movie.id) }}">Edit</a>
<form class="inline-form" method="post" action="{{ url_for('.delete', movie_id=movie.id) }}">
<input class="btn" type="submit" name="delete" value="Delete" onclick="return confirm('Are you sure?')">
</form>
<a class="imdb" href="https://www.imdb.com/find?q={{ movie.title }}" target="_blank" title="Find this movie on IMDb">IMDb</a>
</span>
</li>

0 comments on commit f7f7355

Please sign in to comment.