def addpost(): form = PostForm(request.form) if request.method == 'POST' and form.validate_on_submit(): title = form.title.data content = form.content.data tags = normalize_tags(form.tags.data) entry = Post(title=title, content=content) entry.tags = tags try: entry.store_to_db() flash(u'文章存入成功') except: flash(u'文章存入失败,请与管理员联系') return redirect(url_for('admin.index')) return render_template('admin/addpost.html', form=form)
def edit(id): entry = Post.query.filter_by(id=id).first() if not entry: abort(404) taglist = [] for tag in entry.tags: taglist.append(tag.name) form = PostForm(title=entry.title, content=entry.content, tags=','.join(taglist)) if request.method == 'POST' and form.validate_on_submit(): Post.query.filter_by(id=id).update({ Post.title: request.form['title'], Post.content: request.form['content'], Post.tags: request.form['tags']}) db.session.commit() return redirect(url_for('admin.mnpost')) return render_template('admin/edit.html', form=form)
def post_new(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): new_entry = form.save(commit=False) new_entry.author = request.user split_content = new_entry.content.split(settings.BLOG_CUT) if len(split_content) > 0: new_entry.preview = split_content[0] if 'save' in request.POST: new_entry.is_published = True if 'draft' in request.POST: new_entry.is_published = False new_entry.save() form.save_m2m() return HttpResponseRedirect(reverse('blog_index')) else: form = PostForm() return direct_to_template(request, 'fblog/post_edit.html',{'form':form})
def post_edit(request, year, month, day, slug, **kwargs): post = Post.objects.get(date__year=year, date__month=month, date__day=day, slug=slug) if request.method == 'POST': form = PostForm(request.POST, instance=post) if form.is_valid(): new_entry = form.save(commit=False) new_entry.author = request.user split_content = new_entry.content.split(settings.BLOG_CUT) if len(split_content) > 0: new_entry.preview = split_content[0] if 'save' in request.POST: new_entry.is_published = True if 'draft' in request.POST: new_entry.is_published = False new_entry.save() form.save_m2m() return HttpResponseRedirect(post.get_absolute_url()) else: form = PostForm(instance=post) return direct_to_template(request, 'fblog/post_edit.html', {'form':form,'post':post})