def add_category(id=0): """ add category """ category = Category.query.filter(Category.id == id).first() category_form = CategoryForm(obj=category) if request.method == 'POST' and category_form.validate(): if category: category_form.populate_obj(category) db_session.flush() db_session.commit() else: category = Category(category_form.name.data) category.content = category_form.content.data db_session.add(category) db_session.flush() db_session.commit() return redirect("/c/%s" % category.id) else: return render_template('/topic/add_category.html', category_form=category_form)