def article_edit(id): form = ArticleEditForm() form.category_id.choices = Category.choices() article = Article.find_by_id(int(id)) form.title.data = article.title form.body.data = article.body form.tag.data = article.tag form.category_id.data = article.category_id form.is_open.data = article.is_open form.id.data = id return render_template('article_edit.html', title=u'编辑' + article.title, form=form)
def article_commit(): if g.user.is_admin(): data = request.form title, category, tag, is_open, body, text, art_id = data['title'], data['category'], data['tag'], \ data['open'], data['body'], data['text'], data["id"] if title == '' or tag == '' or body == '' or text == '': return json.dumps({'msg': u'必填字段不能为空'}) if art_id: article = Article.find_by_id(int(id)) article.title = title article.category_id = category article.tag = tag article.is_open = is_open article.body = body article.text = text article.timestamp = datetime.datetime.now() db.session.add(article) else: nowtime = datetime.datetime.now() article = Article(title=title, body=body, user_id=g.user.id, category_id=category, text=text, timestamp=nowtime, tag=tag, is_open=is_open) article.post_date = nowtime try: db.session.add(article) db.session.commit() return json.dumps({'msg': u'保存成功'}) except: return json.dumps({'msg': u'保存失败'}) else: return json.dumps({'msg': u'请用管理员账号登陆'})