html 简单的Flask CRUD应用程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 简单的Flask CRUD应用程序相关的知识,希望对你有一定的参考价值。
{% extends 'layout.html' %}
{% block body %}
<div class="container">
<div class="container">
<div class="row">
<div class="col-sm-2">
<img src="{{data.json.Poster}}" alt="Movie Poster" class="img-fluid">
</div>
<div class="col-sm-10">
<h1>Title: {{data.json.Title}}</h1>
<h2>Director: {{data.json.Director}}</h2>
<h4>Plot: {{data.json.Plot}}</h4>
</div>
</div>
</div>
<form action="/movie/{{data.json.imdbID}}" method="POST" class="form">
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username" class="form-control">
</div>
<div class="form-group">
<label for="review">Review</label>
<textarea name="review" cols="20" rows="3" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<div class="container">
{% for item in data.reviews %}
<b>{{item[2]}}</b>
<p>{{item[3]}}</p>
<a href="/review/edit/{{item[0]}}"><button type="submit" class="btn btn-primary">edit</button></a>
<a href="/review/delete/{{item[0]}}"><button type="submit" class="btn btn-primary">delete</button></button></a>
<hr>
{% endfor %}
</div>
</div>
{% endblock %}
from flask import Flask, request, render_template
import requests
import sqlite3
app = Flask(__name__)
conn = sqlite3.connect("./reviews",check_same_thread=False)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/movie/<mid>', methods=['GET','POST'])
def movie_page(mid):
if request.method == 'GET':
r = requests.get("http://www.omdbapi.com/?i={}&apikey=e389610c".format(mid))
sql = "select * from reviews where movieid='{}'".format(mid)
cur = conn.cursor()
cur.execute(sql)
result = cur.fetchall()
print(result)
return render_template('movie.html', data = {'json': r.json(), 'reviews': result})
elif request.method == 'POST':
username = request.form['username']
review = request.form['review']
print(username, review)
sql = "Insert into reviews(movieid,username,review) values ('{}','{}','{}')".format(mid,username,review)
cur = conn.cursor()
cur.execute(sql)
return "This data shall be pushed"
@app.route('/review/delete/<rid>')
def delete_review(rid):
try:
sql = "delete from reviews where id={}".format(rid)
cur = conn.cursor()
cur.execute(sql)
except:
return "Deleted!"
@app.route('/review/edit/<rid>', methods=['GET','POST'])
def edit_review(rid):
if request.method == 'GET':
return render_template('edit.html',rid=rid)
elif request.method == 'POST':
review = request.form['review']
print(review)
sql = "update reviews set review='{}' where id={}".format(review, rid)
cur = conn.cursor()
cur.execute(sql)
return "Review is updated"
if __name__=='__main__':
app.run(debug=True)
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Review meter</title>
</head>
<body>
{% block body %}
{% endblock %}
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
{% extends 'layout.html' %}
{% block body %}
<h1>Welcome to Review Meter!</h1>
{% endblock %}
{% extends 'layout.html' %}
{% block body %}
<h1>Edit your review!</h1>
<form action="/review/edit/{{rid}}" method="POST" class="form">
<div class="form-group">
<label for="review">Review</label>
<textarea name="review" cols="20" rows="3" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% endblock %}
以上是关于html 简单的Flask CRUD应用程序的主要内容,如果未能解决你的问题,请参考以下文章