def travels_edit(id=None): """ 编辑游记 """ form = TravelsForm() form.scenic_id.choices = [(v.id, v.title) for v in Scenic.query.all()] form.submit.label.text = "修改" travels = Travels.query.get_or_404(int(id)) if request.method == "GET": form.scenic_id.data = travels.scenic_id form.content.data = travels.content if form.validate_on_submit(): data = form.data travels_count = Travels.query.filter_by(title=data["title"]).count() # 判断是否有重复数据 if travels_count == 1 and travels.title != data["title"]: flash("游记已经存在!", "err") return redirect(url_for('admin.travels_edit', id=id)) travels.title = data["title"] travels.scenic_id = data["scenic_id"] travels.author = data["author"] travels.content = data["content"] db.session.add(travels) db.session.commit() flash("修改景区成功!", "ok") return redirect(url_for('admin.travels_edit', id=id)) return render_template("admin/travels_edit.html", form=form, travels=travels)
def travels_add(): """ 添加游记 """ form = TravelsForm() form.scenic_id.choices = [(v.id, v.title) for v in Scenic.query.all()] if form.validate_on_submit(): data = form.data # 判断游记是否存在 travels_count = Travels.query.filter_by(title=data["title"]).count() # 判断是否有重复数据。 if travels_count == 1 : flash("景点已经存在!", "err") return redirect(url_for('admin.travels_add')) travels = Travels( title=data["title"], author = data["author"], scenic_id = data["scenic_id"], content=data["content"], ) db.session.add(travels) db.session.commit() addOplog("添加游记"+data["title"]) # 添加日志 flash("添加游记成功!", "ok") return redirect(url_for('admin.travels_add')) return render_template("admin/travels_add.html", form=form)