def index(): #name = {'username' : current_user.username} form = TweetForm() if form.validate_on_submit(): t = Tweet(body=form.tweet.data, author=current_user) db.session.add(t) db.session.commit() return redirect(url_for('index')) page_num = int(request.args.get('page') or 1) #这里我就能知道我当前是第几页然后我们就能根据当前的page index 来翻页 tweets = current_user.own_and_followed_tweets().paginate( page=page_num, per_page=current_app.config['TWEET_PER_PAGE'], error_out=False) #这里tweets是一个page的对象,里面有next num,然后显示在网页上的是tweets.items if tweets.has_next: next_url = url_for('index', page=tweets.next_num) else: next_url = None if tweets.has_prev: prev_url = url_for('index', page=tweets.prev_num) else: prev_url = None return render_template('index.html', tweets=tweets.items, form=form, next_url=next_url, prev_url=prev_url)
def index(): form=TweetForm() if form.validate_on_submit(): #如果是用post到首頁 t = Tweet(body=form.tweet.data, author=current_user) #Tweet表單 db.session.add(t) db.session.commit() return redirect(url_for('index')) title="Twittor首頁" page_num = int(request.args.get('page') or 1) #接收網址get的?page=頁數 默認在第一頁_ # name={'username': current_user.username} 已改直接寫到模板裡了 #posts=[ # { # 'author':{'username':'******'},'body':"Hi! I'm Root!" # },{ # 'author':{'username':'******'},'body':"Hollo! I'm Tester." # },{ # 'author':{'username':'******'},'body':"Hi! I'm Tester2." # } #] tweets = current_user.own_and_followed_tweets().paginate( page=page_num, per_page=current_app.config['TWEET_PER_PAGE'], error_out=False) #分頁語法paginate(開始頁=page_num,每頁幾則=5,沒內容時顯示404=Flase) next_url=url_for('index',page=tweets.next_num) if tweets.has_next else None prev_url=url_for('index',page=tweets.prev_num) if tweets.has_prev else None #如果有下一頁才有參數否則為None return render_template('index.html', tweets=tweets.items, title=title ,form=form, next_url=next_url , prev_url=prev_url )
def index(): form = TweetForm() if form.validate_on_submit(): t = Tweet(body=form.tweet.data, author=current_user) db.session.add(t) db.session.commit() return redirect(url_for('index')) page_num = int(request.args.get('page') or 1) tweets = current_user.own_and_followed_tweets().paginate( page=page_num, per_page=current_app.config['TWEET_PER_PAGE'], error_out=False) next_url = url_for('index', page=tweets.next_num) if tweets.has_next else None prev_url = url_for('index', page=tweets.prev_num) if tweets.has_prev else None return render_template('index.html', form=form, tweets=tweets.items,\ next_url=next_url, prev_url=prev_url)
def index(): form = TweetForm() if form.validate_on_submit(): t = Tweet(body=form.tweet.data, author=current_user) db.session.add(t) db.session.commit() return redirect(url_for('index')) page_num = int(request.args.get('page') or 1) tweets = current_user.own_and_followed_tweets().paginate( page=page_num, per_page=current_app.config['TWEET_PER_PAGE'], error_out=False) next_url = url_for('index', page=tweets.next_num) if tweets.has_next else None prev_url = url_for('index', page=tweets.prev_num) if tweets.has_prev else None name = {'username': current_user.username} # posts = [ # { # 'author': {'username':'******'}, # 'body':"Hi I am a root" # }, # { # 'author': {'username':'******'}, # 'body':"Hi I am a tset" # }, # { # 'author': {'username':'******'}, # 'body':"Hi I am a test2" # } # ] return render_template('index.html', tweets=tweets.items, form=form, next_url=next_url, prev_url=prev_url)