def edit_article(id): article = Article.query.filter_by(id=id).first() articleform = AritcleForm(title=article.title,context=article.context,created_at=article.created_at,view_count=article.view_count) tags = Tag.query.all() categorys = Category.query.all() if articleform.validate_on_submit(): article_title = request.form.get('title') article_context = request.form.get('context') article_time = request.form.get('created_at') article_viewcount = request.form.get('view_count') article_category = request.form.get('category') article_tags = request.form.getlist('tags') category = Category.query.filter_by(name=article_category).first() tags = gen_tags(article_tags) print(article_time) try: article = Article.query.filter_by(id=id) article.update({Article.title:article_title,Article.context:article_context,Article.created_at:article_time,Article.view_count:article_viewcount,Article.category_id:category.id}) article.first().tags = tags db.session.commit() except: flash("edit_category Database error!") return render_template("admin/editarticle.html",articleform=articleform,article=article,tags=tags,categorys=categorys) return redirect(url_for("list_article")) return render_template("admin/editarticle.html",articleform=articleform,article=article,tags=tags,categorys=categorys)
def add_article(): articleform = AritcleForm() tags = Tag.query.all() categorys = Category.query.all() if articleform.validate_on_submit(): article_title = request.form.get('title') article_context = request.form.get('context') article_time = request.form.get('created_at') article_viewcount = request.form.get('view_count') article_category = request.form.get('category') article_tags = request.form.getlist('tags') category = Category.query.filter_by(name=article_category).first() tags = gen_tags(article_tags) article = Article(title = article_title,context = article_context,created_at = article_time,view_count=article_viewcount,category_id=category.id,tags=tags) try: db.session.add(article) db.session.commit() except: flash("add_article Database error!") return render_template("admin/addarticle.html",articleform=articleform,tags=tags,categorys=categorys) return redirect(url_for('list_article')) return render_template("admin/addarticle.html",articleform=articleform,tags=tags,categorys=categorys)