mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-30 11:19:57 +00:00
Update views.py
Fixed numbers plates not disappearing when submitting an existing number plate
This commit is contained in:
@@ -25,30 +25,26 @@ def add():
|
|||||||
|
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
plate = form.numberplate.data
|
plate = form.numberplate.data
|
||||||
print("test")
|
if plate: # To prevent red lines in VSCode
|
||||||
if AllowedPlate.query.filter_by(plate=plate).first():
|
# Check if number plate already exists
|
||||||
print("test1")
|
if AllowedPlate.query.filter_by(plate=plate).first():
|
||||||
flash("Numberplate is already registered")
|
flash("Numberplate is already registered")
|
||||||
return render_template(
|
else: # NP does not exist
|
||||||
"dashboard.html",
|
ap = AllowedPlate(plate=plate)
|
||||||
form=npForm(formdata=None),
|
db.session.add(ap)
|
||||||
feedback="Numberplate is already registered",
|
db.session.commit()
|
||||||
)
|
|
||||||
ap = AllowedPlate(plate=plate)
|
|
||||||
db.session.add(ap)
|
|
||||||
db.session.commit()
|
|
||||||
print("test2")
|
|
||||||
flash("Numberplate succesfully added")
|
|
||||||
|
|
||||||
# Update the list on the page with JavaScript
|
# Update the list on the page with JavaScript
|
||||||
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
|
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
|
||||||
plates = AllowedPlate.query.order_by(AllowedPlate.id).all()
|
plates = AllowedPlate.query.order_by(AllowedPlate.id).all()
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"plates": [
|
"plates": [
|
||||||
{"id": p.id, "plate": p.plate} for p in plates
|
{"id": p.id, "plate": p.plate} for p in plates
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
Plates = AllowedPlate.query.order_by(AllowedPlate.id).all()
|
# form wasn't valid
|
||||||
return render_template("add.html", form=npForm(formdata=None), plates=Plates)
|
return render_template(
|
||||||
|
"add.html", form=npForm(formdata=None), plates=Plates
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user