def add_pet(): """Shows add pet form; handles form submit.""" form = AddPetForm() if form.validate_on_submit(): data = { key: value for key, value in form.data.items() if key not in ["csrf_token", "photo_url", "photo_upload"] } pet = Pet(**data) if form.photo_upload.data: f = form.photo_upload.data filename = secure_filename(f.filename) f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) pet.photo = url_for('static', filename=filename) elif form.photo_url.data: pet.photo = form.photo_url.data db.session.add(pet) db.session.commit() flash(f"Added {pet.name}") return redirect("/") else: return render_template("add_pet.html", form=form)