曾尝试对此进行研究,但我找不到答案。
在该代码下,它显示为:关键错误:PurchaseDate
我的代码计划在其下运行照相机数据库,用户在表格中填写购买日期以及其他详细信息。输入类型是日期,所以我想知道这可能是个问题吗?我的数据库位于Navicat MYSQL中,其确切拼写和标点与“ PurchaseDate”相同。
这是我的app.py代码:
from flask import Flask, render_template, request, redirect, session,url_for
import pymysql, datetime, os
app = Flask(__name__)
# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app
#database connection function
def create_connection():
return pymysql.connect(
host='',
user='',
password='ACCENTUATE',
db='benwilsoncameras',
charset='utf8mb4'
,cursorclass=pymysql.cursors.DictCursor
)
@app.route('/')
def home():
"""Renders a sample page."""
return render_template("index.html",title="Home")
# read or list records in cameras table
@app.route("/cameras", methods = ["GET", "POST"])
def cameras():
connection=create_connection()
with connection.cursor() as cursor:
sql="SELECT * From tblcameras ORDER By CameraID DESC"
cursor.execute(sql)
#fetch all cameras into a list
cameras = cursor.fetchall()
return render_template("cameras.html", cameras = cameras, title="cameras Listing")
#return redirect(url_for('cameras'))
# create
@app.route("/new_camera", methods = ["GET", "POST"])
def newcamera():
connection=create_connection()
if request.method =="POST":
get = request.form
Company = get["Company"]
Model = get["Model"]
PurchaseDate = get["PurchaseDate"]
Condition = get["Condition"]
KitLens = get["KitLens"]
PurchasePrice = get["PurchasePrice"]
#photo=
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `tblcameras` (Company, Model, PurchaseDate, Condition, KitLens, PurchasePrice) VALUES (%s,%s,%s,%s,%s,%s)"
val=(Company, Model, PurchaseDate, Condition, KitLens, PurchasePrice)
cursor.execute(sql, val)
#save values in dbase
connection.commit()
cursor.close()
return redirect("/cameras")
return redirect(url_for('cameras?do=new', title="Add New camera"))
#return render_template("cameras.html",title="Adding New camera")
#camera
# edit
@app.route("/edit_camera", methods = ["GET", "POST"])
def editcamera():
camera_id = request.args.get('id')# get the id parameter value
connection=create_connection()
if request.method =="POST":
get = request.form
Company = get["Company"]
Model = get["Model"]
#picture=
with connection.cursor() as cursor:
# Update record
update_sql = "UPDATE cameras SET cameras.Company = %s,cameras.Model=%s WHERE cameras.CameraID = %s"
values=(Company,Model,camera_id)
cursor.execute(update_sql,(values))
#save or commit values in dbase
connection.commit()
cursor.close()
return redirect("/cameras")
return render_template("camera.html", title ="Editing New camera")
#details
@app.route("/camera")
def camera():
CameraID = request.args.get('id')# get the id parameter value
connection=create_connection()
with connection.cursor() as cursor:
sql="SELECT * From tblcameras WHERE CameraID=%s"
values=(CameraID)
cursor.execute(sql, (values))
#fetch camera with specified Id
camera = cursor.fetchone()
return render_template("camera.html", camera=camera)
# delete
@app.route("/delete_camera", methods = ["GET", "POST"])
def deletecamera():
camera_id = request.args.get('id')# get the id parameter value
connection=create_connection()
if request.method =="POST":
get = request.form
CameraID = get["Id"]
with connection.cursor() as cursor:
# delete record
delete_sql = "DELETE FROM tblcameras WHERE CameraID=%s"
value=(int(camera_id),)
cursor.execute(delete_sql,value)
#save values in dbase
connection.commit()
print(cursor.rowcount, "record(s) deleted")
cursor.close()
return redirect("/cameras")
return render_template("camera.html", title ="Deleting camera")
if __name__ == '__main__':
import os
HOST = os.environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT,debug=True)
还有我的HTML,您在哪里制作了新相机,我觉得错误可能在这里?
{% extends "layout.html" %}
{% block content %}
{% if (request.args.get('do')=='new' )%}
<br />
<h2>New camera</h2>
<form method="post" action="/new_camera" class="was-validated">
<div class="form-group">
<label for="Company">Company:</label>
<select id="Company" name="Company" class="form-control">
<option value="">Choose an option</option>
<option value="Canon">Canon</option>
<option value="Nikon">Nikon</option>
</select>
<div class="valid-feedback">Valid.</div>
<div class="invalid-feedback">Please fill out this field.</div>
</div>
<div class="form-group">
<label for="Model">Model:</label>
<input type="text" class="form-control" id="Model" placeholder="Enter Model" name="Model" required>
<div class="valid-feedback">Valid.</div>
<div class="invalid-feedback">Please fill out this field.</div>
</div>
<div class="form-group">
<label for="PurchaseDate">Purchase Date:</label>
<input type="date" class="form-control" id="PurchaseDate" placeholder="Purchase Date" name="PurchaseDate" required>
<div class="valid-feedback">Valid.</div>
<div class="invalid-feedback">Please fill out this field.</div>
</div>
<div class="form-group">
<label for="Condition">Condition:</label>
<input type="text" class="form-control" id="Condition" placeholder="Enter Condition" name="Condition" required>
<div class="valid-feedback">Valid.</div>
<div class="invalid-feedback">Please fill out this field.</div>
</div>
<div class="form-group">
<label for="Kit Lens">Kit Lens</label>
<input type="text" class="form-control" id="Kit Lens" placeholder="Enter Kit Lens (mm) or Standard" name="Kit Lens" required>
<div class="valid-feedback">Valid.</div>
<div class="invalid-feedback">Please fill out this field.</div>
</div>
<div class="form-group">
<label for="Purchase Price">Purchase Price</label>
<input type="number" min="1" step="any" class="form-control" id="Purchase Price" placeholder="Enter Purchase Price ($)" name="Purchase Price" required>
<div class="valid-feedback">Valid.</div>
<div class="invalid-feedback">Please fill out this field.</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<hr />
{% endif %}
<br />
<h2>
Existing cameras
</h2>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
{%for camera in cameras%}
<tr>
<td>{{camera.Company}} {{camera.Model}}</td>
<td>
<a href="/camera?do=edit&id={{camera.cameraId}}" class="btn btn-info">Edit</a>
<a href="/camera?do=details&id={{camera.cameraId}}" class="btn btn-success">Details</a>
<a href="/camera?do=delete&id={{camera.cameraId}}" class="btn btn-danger">Delete</a></td>
</tr>
{%endfor%}
</table>
</div>
{% endblock %}
我的Navicat MYSQL数据库应该输入这些详细信息以及它的“ CameraID”,“公司”,“模型”,“购买日期”,“条件”,“ KitLens”,“购买价格”。