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('index')) posts = current_user.followed_posts().all() return render_template('index.html', title='Home', form=form, posts=posts)
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('Your post is now live!') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate(page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) if posts.has_prev else None return render_template('index.html', title='Home', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(page=1): print "="*20 with toolapp.test_request_context(): print url_for("index") form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, timestamp=datetime.utcnow(), author=current_user) db.session.add(post) db.session.commit() flash('your post now is live!') return redirect(url_for('index')) posts = current_user.followed_posts().paginate(page, config.POSTS_PER_PAGE, False) # flash(g.user.followed_posts().all()) # posts = g.user.followed_posts().all() return render_template('index.html', title='Home', form=form, posts=posts)
def index(): # New Post form = GratefulForm() placeholders = random.sample(app.config['PLACEHOLDERS'], 3) if form.validate_on_submit(): if current_user.is_authenticated: models.create_post( current_user, [form.item1.data, form.item1.data, form.item1.data]) else: return redirect(url_for('register')) # Feed feed = None if current_user.is_authenticated: feed = current_user.followed_posts() return render_template('index.html', grateful_form=form, placeholders=placeholders, feed=feed)
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('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None return render_template( "index.html", title=_("Home"), posts=posts.items, form=form, 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() # _() mark for language translation flash(_('Your post is now live!')) # Post/Redirect/Get - avoids inserting duplicate posts return redirect(url_for('main.index')) #paginate, next and previous page links 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=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): page = request.args.get('page', 1, type=int) user_posts = Post.query.filter_by(user_id=current_user.id).paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for( 'index', page=user_posts.next_num) if user_posts.has_next else None prev_url = url_for( 'index', page=user_posts.prev_num) if user_posts.has_prev else None followed_posts = current_user.followed_posts() # ------------------------ Default Page View ----------------------------------------------------------------------- if request.method == 'GET': return render_template('index.html', title='Home', user=current_user, user_posts=user_posts.items, followed_posts=followed_posts, next_url=next_url, prev_url=prev_url) else: pass
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('Your post is now live!') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev.num) \ if posts.has_prev else None return render_template('index.html', title='Home Page', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): form = PostForm() if form.validate_on_submit(): language_text = TextBlob(form.post.data) language = language_text.detect_language() 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=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): dispatches = Dispatch.query.filter_by(user_id=current_user.id).all() if request.method == 'POST' and request.form.get('post') is not None: dispatch = Dispatch.query.filter_by(name=request.form.get('savedSchemas')).first() flash(request.form.get('savedSchemas')) if dispatch is not None: dispatch_id = dispatch.id else: dispatch_id = None post = Post(body=request.form.get('post'), user_id=current_user.id, username=current_user.username, dispatch_id=dispatch_id) db.session.add(post) db.session.commit() params.update({'user': current_user}) posts = current_user.followed_posts().all() print(posts) post_params = [] for user, post, dispatch in posts: if dispatch is not None: post_params.append({"user_id": user.id, "user_name": user.username, "post_body": post.body, "post_timestamp": post.timestamp, "dispatch_name": dispatch.name, "dispatch_body": url_for('results') + '/' + quote(dispatch.body.replace('$$$', '').replace('[', '/[')) }) print(post_params[-1]['dispatch_body']) else: post_params.append({"user_id": user.id, "user_name": user.username, "post_body": post.body, "post_timestamp": post.timestamp}) params.update({'posts': dumps(post_params, default=str)}) # print(dispatches) return render_template('index.html', params=params, dispatches=dispatches)
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('index') ) # mitigates annoyance w/ refresh command implemented in web browsers - new request is GET, not POST (Post/Redirect/Get pattern) # posts = [ # {'author': {'username': '******'}, # 'body': 'Beautiful day in Portland!'}, # {'author': {'username': '******'}, # 'body': 'The Avengers movie was so cool!'} # ] # posts = current_user.followed_posts().all() ... calling all() triggers execution of SQLAlchemy query object page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False ) # False means empty list for non-existing page, not 404 error (True) if posts.has_next: next_url = url_for('index', page=posts.next_num) else: next_url = None if posts.has_prev: prev_url = url_for('index', page=posts.prev_num) else: prev_url = None return render_template('index.html', title=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url) # .items is because of paginate
def home(): form_obj = PostForm() if form_obj.validate_on_submit(): # Funcionality to guees de language of submitted post lan = guessLanguage(form_obj.post.data) print(lan) if lan == 'UNKNOWN' or len(lan) > 5: lan = '' post= Post(body=form_obj.post.data, author=current_user, lan = lan) db.session.add(post) db.session.commit() flash('Great!') return redirect(url_for('main.home')) page = request.args.get('page', 1, type = int) if current_user.is_authenticated: posts = current_user.followed_posts().paginate(page, app.config['POSTS_PER_PAGE'], False) # Next url next_url = url_for('main.home', page = posts.next_num) if posts.has_next else None prev_url = url_for('main.home', page = posts.prev_num) if posts.has_prev else None return(render_template('home.html', title = 'Home', form = form_obj, posts = posts.items, next_url = next_url, prev_url = prev_url)) return(render_template('home.html', title = 'Explore', form = form_obj))
def index(): form = PostForm() base_configuration = base_navigation.query.all() apnews = newsPost.query.order_by(newsPost.timestamp.desc()).limit(5) mvnews = movenewsPost.query.order_by(movenewsPost.timestamp.desc()).limit(5) oanews = onearticlePost.query.order_by(onearticlePost.timestamp.desc()).limit(5) if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) 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=_('Home'), form=form, apnews=apnews,mvnews=mvnews,oanews=oanews, posts=posts.items, next_url=next_url, prev_url=prev_url,base_configuration=base_configuration)
def index(): form = PostForm() if form.validate_on_submit(): # Use the guess_language package 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!')) # This redirect is here avoids resubmitting the form on page # refresh. See the wiki on 'Post/Redirect/Get'. return redirect(url_for('main.index')) # Paginate results, which means to limit results to X items per page # instead all at once. Flask supports it, and we use it in the URL. page = request.args.get('page', 1, type=int) # arg1 = page number, starting at 1 # arg2 = # items per page # arg3 = True: return 404 to client; False: return empty list posts = current_user.followed_posts().paginate( page, current_app.config['POSTS_PER_PAGE'], False) # Ternarys to set next and prev url link, if they exist # The keyword args on url_for() are kind of like React props, # in that the recieving template will recieve via Flask putting # them in the URL as query args. 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 # Render the template, substituting variables when needed. return render_template('index.html', title=_('Home Page'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): form = PostForm() if form.validate_on_submit(): note = Note(body=form.note.data, author=current_user) db.session.add(note) db.session.commit() flash('Your note has been received!') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) notes = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=notes.next_num) \ if notes.has_next else None prev_url = url_for('index', page=notes.prev_num) \ if notes.has_prev else None return render_template('index.html', title='Home', form=form, notes=notes.items, next_url=next_url, prev_url=prev_url)
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('index')) posts = current_user.followed_posts().all() return render_template("index.html", title='Home Page', form=form, posts=posts) return render_template("index.html", title='Home Page', form=form, posts=posts)
def index(): form = CommentForm() 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('posts.index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('posts.index', page=posts.prev_num) \ if posts.has_prev else None if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post, author=current_user._get_current_object()) db.session.add(comment) db.session.commit() return render_template('posts/index.html', title='Followed Posts', posts=posts.items, form=form, next_url=next_url, prev_url=prev_url)
def index(): form = PostForm() if form.validate_on_submit(): language = guess_language(form.post.data) # guess the language of the post, if unknown or unexpected long it is treated as unknown 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 has been published')) return redirect(url_for('main.index')) page = request.args.get('page', 1, type=int) # paginate object returns .items with the content for a page, # but also properties such as has_next, has_prev and next_num and prev_num 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=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): # user = {'username': '******'} # posts = [ # { # 'author': {'username': '******'}, # 'body': 'Demo Flask with some seconds' # }, # { # 'author': {'username': '******'}, # 'body': 'Code back-end for production' # } # ] # ==== FORM POST ==== form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) # posts = current_user.followed_posts().all() # Tham số 1: số thứ tự trang hiện tại, bắt đầu từ 1 # Tham số 2: Số đơn vị trên một trang # Tham số 3: Cờ hiệu lỗi, True - 404, False trả về rỗng posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None return render_template('index.html', title='Home Page', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): post_form = PostForm() empty_form = EmptyForm() if post_form.validate_on_submit(): create_post(body=post_form.post.data) 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=_('Home'), post_form=post_form, empty_form=empty_form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): form = PostForm() if form.validate_on_submit(): language = detect_lang(form.post.data)['lang'] 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=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def myaccount(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Din post är skickat!') return redirect(url_for('myaccount')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('myaccount', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('myaccount', page=posts.prev_num) \ if posts.has_prev else None return render_template('myaccount.html', drop_down_cats=drop_down_cats, title='Mitt Konto', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): form = PostForm() if form.validate_on_submit(): language = guess_language(form.post.data) # to handle what the language is and save to the db-table 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!')) # it will be GET method of request when client refresh current page, instead POST! return redirect(url_for('bp.index')) # Post/Redirect/Get schema 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('bp.index', page=posts.next_num) if posts.has_next else None prev_url = url_for('bp.index', page=posts.prev_num) if posts.has_prev else None return render_template('index.html', title='Home Page', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): """ Home Page view function, where you can see posts and make posts of your own. """ form = PostForm() if form.validate_on_submit(): post.Post(body=form.post.data, author=current_user) db.session.add(post) db.session.comit() flash("Your post is now live!") # we use the simple Post/Redirect/Get pattern trick to avoid # inserting duplicate posts when a user inadvertently refreshes the page # after submitting a webform. return redirect(url_for("index")) # pagination of posts on the front page of all posts # of users current_user is following, including own, # ordered retro-chronoclogically page = requests.args.get("page", 1, type=int) # load N posts per page using pagination posts = current_user.followed_posts().paginate( page, app.config["POSTS_PER_PAGE"], False) # previous page url prev_url = url_for("index", page=posts.prev_num) if posts.has_prev else None # next page url next_url = url_for("index", page=posts.next_num) if posts.has_next else None return render_template( "index.html", title="Home", form=form, posts=posts.items, prev_url=prev_url, next_url=next_url, )
def user(username,page=1): userController = UserController() user = userController.query_byname(username=username) if user == None: flash('User ' + username + ' not found.') return redirect('/') #getposts followed_posts_results = current_user.followed_posts(page=page) posts = followed_posts_results['posts'] has_pre_page = followed_posts_results['has_pre_page'] pre_page_num = followed_posts_results['pre_page_num'] has_next_page = followed_posts_results['has_next_page'] next_page_num = followed_posts_results['next_page_num'] template_naeme = 'user.html' return render_template(template_naeme, title='Posts', posts=posts, user=user, has_next_page=has_next_page, next_page_num=next_page_num, has_pre_page=has_pre_page, pre_page_num=pre_page_num)
def index(): #编辑并提交博文 form = PostForm() if form.validate_on_submit() and current_user.can( Permission.WRITE_ARTICLES): #数据库需要真正的用户对象 current_user由Flask-Login提供 #和所有上下文变量一样 通过线程内的代理对象实现 #该对象表现类似用户对象 实际上是一个轻度包装 包含真正的用户对象 post = Post(body=form.body.data, author=current_user._get_current_object()) db.session.add(post) return redirect(url_for("main.index")) #按时间戳降序排列 #posts=Post.query.order_by(Post.timestamp.desc()).all() page = request.args.get('page', 1, type=int) show_followed = False if current_user.is_authenticated(): show_followed = bool(request.cookies.get('show_followed', '')) if show_followed: query = current_user.followed_posts() else: query = Post.query #返回一个Paginate()对象 该类在SQLAlchemy里面实现 pagination = query.order_by(Post.timestamp.desc()).paginate( page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'], error_out=False) posts = pagination.items return render_template("index.html", form=form, posts=posts, show_followed=show_followed, pagination=pagination)
def home(): users = [] form = RegistrationForm() if form.validate_on_submit(): firstName = form.firstName.data.capitalize() lastName = form.lastName.data.capitalize() email = form.email.data.lower() hashed_password = bcrypt.generate_password_hash( form.password.data).decode( 'UTF-8') # Encrypt the password stored in form.password.data user = User(FirstName=firstName, LastName=lastName, Email=email, Password=hashed_password) db.session.add(user) db.session.commit() # Login the user with the session duration set login_user(user, duration=timedelta) # Second argument is optional, uses to assign what category the message is flash('Signed in!', 'success') return redirect(url_for('main.home')) if current_user.is_authenticated: posts = current_user.followed_posts().all() # users = User.query.filter(User.id != current_user.id).order_by( # func.random()).limit(3).all() tempUsers = User.query.filter(User.id != current_user.id).order_by( func.random()).all() for tempUser in tempUsers: if not current_user.is_following(tempUser) and len(users) <= 2: users.append(tempUser) return render_template('user-index.html', posts=posts, users=users, active='home') # posts = Post.query.order_by(Post.DatePosted.desc()) return render_template('index.html', form=form)
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) db.session.add(post_) db.session.commit() flash('Your post is now live!') # this is a standard. Respond to a POST request of a web form submission with a redirect 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='Home Page', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): # user = {'username': '******'} 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('index')) # 假帖子 # posts = [ # { # 'author': {'username': '******'}, # 'body': '武汉,每天不一样!' # }, # # { # 'author': {'username': '******'}, # 'body': '武汉的东湖很美丽!' # } # ] # 真帖子 page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None return render_template( "index.html", title='我的', # user=user, 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.body.data, anonymous=form.anonymous.data, # author=current_user) # db.session.add(post) # db.session.commit() # flash('Your post is now live!') # return redirect(url_for('index')) username = user print(user) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate(page, app.config['POSTS_PER_PAGE'], False) users = current_user.followed_users() next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None return render_template('home.html', title='Home', # form=form, posts=posts.items, users=users, next_url=next_url, prev_url=prev_url)
def index(): '''posts = [ {'author': {'username': '******'}, 'body': 'Beautiful day in Portland!'}, {'author': {'username': '******'}, 'body': 'The Avengers movie was so cool!'} ]''' form = PostForm() if form.validate_on_submit(): post = Post(author=current_user, body=form.post.data) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) # posts = current_user.followed_posts() next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None return render_template('index.html', form=form, title='Home', posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): """Funkcja wyświetlająca strone główną aplikacji wraz z postami obserwowanych użytkowników.""" form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash(_('Succesfully added a post!')) return redirect(url_for('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None return render_template('index.html', title=_("Home"), 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('Post created') # post/redirect/get pattern return the user to index page as a refresh, to avoid inserting duplicate posts return redirect(url_for('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) if posts.has_prev else None return render_template('index.html', title='Home', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): # API - Random response_random = requests.get( 'https://www.thecocktaildb.com/api/json/v1/1/random.php') random_drink = response_random.json() response_category = requests.get( 'https://www.thecocktaildb.com/api/json/v1/1/filter.php?c=Cocktail') category_drink = response_category.json() # print(category_drink) # data=data['drinks'] # category_drink=category_drink['drinks']) form = PostForm() 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 if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('main.index')) return render_template('index.html', title="Home", active_page="home", posts=posts.items, form=form, next_url=next_url, prev_url=prev_url, random_drink=random_drink['drinks'], category_drink=category_drink['drinks'])
def home(): error = None form = MessageForm(request.form) if form.validate_on_submit(): new_message = BlogPost( form.title.data, form.description.data, current_user.id, datetime.now() ) db.session.add(new_message) db.session.commit() flash('New entry was successfully posted. Thanks.') return redirect(url_for('home.home')) else: posts = current_user.followed_posts() return render_template( 'index.html', username=current_user.name, posts=posts, form=form, error=error )