Update views.py

Fixed numbers plates not disappearing when submitting an existing number plate
This commit is contained in:
2025-06-02 12:52:16 +02:00
parent 728ddcd28b
commit 8214e36a64

View File

@@ -25,20 +25,14 @@ 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
# Check if number plate already exists
if AllowedPlate.query.filter_by(plate=plate).first(): if AllowedPlate.query.filter_by(plate=plate).first():
print("test1")
flash("Numberplate is already registered") flash("Numberplate is already registered")
return render_template( else: # NP does not exist
"dashboard.html",
form=npForm(formdata=None),
feedback="Numberplate is already registered",
)
ap = AllowedPlate(plate=plate) ap = AllowedPlate(plate=plate)
db.session.add(ap) db.session.add(ap)
db.session.commit() 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":
@@ -50,5 +44,7 @@ def add():
] ]
} }
) )
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
)