def new(): form = PostForm(request.form) form.pub_date.data = datetime.now() form.categories.choices = [(c.id, c.title) for c in Category.query.all()] if form.validate_on_submit(): post = Post() post.title = form.title.data post.slug = form.slug.data.lower() post.status = form.status.data post.body = form.body.data post.pub_date = form.pub_date.data post.categories = Category.query.filter(Category.id.in_(form.categories.data)).all() db.session.add(post) db.session.commit() return redirect(url_for('admin.edit', post_id=post.id)) return render_template('admin/edit.html', form=form)