def index(): form = PostForm() if form.validate_on_submit(): language = guess_language(form.post.data) if language == 'UNKNOWN' or len(language) > 5: language = '' post = Post(body=form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash('your post is now live') return redirect(url_for('main.index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev else None return render_template('index.html', title='Explore', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('提交成功') return redirect(url_for('main.index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate(page, current_app.config['POSTS_PER_PAGE'], False) # 获取下一页的url,next_num:下一页的页码 next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None # 获取上一页的url,prev_num:上一页的页码 prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev else None return render_template('index.html', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def edit(id): ''' 编辑文章 :param id: :return: ''' post = Post.query.get_or_404(id) if current_user != post.author and not current_user.can(Permission.ADMIN): abort(403) form = PostForm() if form.validate_on_submit(): post.body = form.body.data db.session.add(post) db.session.commit() flash('文章已经修改') return redirect(url_for('.post', id=post.id)) form.body.data = post.body return render_template('edit_post.html', form=form)
def register_source(): form = PostForm() if form.validate_on_submit(): sources = Post(title=form.title.data, tag=form.tag.data, category=form.category.data, city=form.city.data, state=form.state.data, country=form.country.data, description=form.description.data, sphere=form.sphere.data, officialLink=form.officialLink.data, author=current_user) db.session.add(sources) db.session.commit() flash(_('Você registrou uma nova Fonte de Dados Abertos')) return redirect(url_for('main.explore')) return render_template('register_source.html', title=(_('Cadastrar Fonte')), form=form)