def index(): # 原始代码 # context = { # 'questions':Question.query.order_by('create_time').all() #注意all() # } # # print(context) # return render_template('index.html',**context) #**的使用 # print(request.args.get('page')) #分页代码 # print(request.args.get('page',1)) page = int(request.args.get('page', 1)) per_page = int(request.args.get('per_page', 4)) # 上面两行千万别丢了 # page = 1 # per_page = 3 paginate = Question.query.order_by(db.desc(Question.create_time)).paginate( page, per_page, error_out=False) #这行代码首先根据“db.desc(Question.create_time)“按照创建时间拍个序号, #然后“.paginate(page, per_page, error_out=False)”就成,没啥特殊操作。 questions = paginate.items # 将项目与paginate对应 question_max_id = Question.query.order_by(db.desc(Question.id)).first().id context = { 'paginate': paginate, 'questions': questions, 'question_max_id': question_max_id #这里我加入了一个最大序号,是为了将最新的条目加“new”标记。 } return render_template('index.html', **context) # **的使用。
def index(): try: sql = mysql() # 连接爬虫数据库,并初始化游标 dataSql = sql.selectTop10('slideright1') datajwc = sql.selectTop10('jwcarticlehtml') #datatencent = sql.select('tencentnews') #print datajwc[0][2] sql.conn.close() #print datajwc context = { 'questions': Question.query.order_by(db.desc(Question.create_time)).all() } allq = Question.query.order_by(db.desc(Question.create_time)).all() picids = {} for q in allq: picids[q.id] = str(q.author.picid) imgs = {} count = 1 for quesion in questions: if (count>6): break if (question.table=='旅游'): soup = BeautifulSoup(quesion.content,'html.parser') content = soup.select('p') img = content[0] imgway = "http://today.hit.edu.cn" + img['src'] imgs[count] = imgway count+=1 return render_template('index.html', dataSql=dataSql,datajwc=datajwc,usermsg=g.user, picid=str(g.user.picid), picids=picids, imgs=imgs, **context)
def get_comment(id_, type_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) if type_ == 0 or type_ == 1: video = db.session.query(Video).filter_by(id=id_).first() comments = [] if video: if type_ == 1: all_comment = db.session.query(Comment).filter_by(target=id_, replay_id=None) \ .order_by(db.desc(Comment.likes_user)).all() else: all_comment = db.session.query(Comment).filter_by(target=id_, replay_id=None) \ .order_by(db.desc(Comment.upload_time)).all() else: return params_error(message="未找到视频") if all_comment: for comment_item in all_comment: is_liked = False replay_to = db.session.query(Comment).filter_by( id=comment_item.replay_id).first() if replay_to: user = db.session.query(User).filter_by( id=replay_to.uid).first() if comment_item.likes_user: likes = list(map(int, comment_item.likes_user.split(','))) if g.user.uid in likes: is_liked = True author = db.session.query(User).filter_by( id=comment_item.uid).first() comment = { 'id': comment_item.id, 'content': comment_item.content, 'likes': len(list(map(int, comment_item.likes_user.split(',')))) if comment_item.likes_user else 0, 'time': comment_item.upload_time.strftime('%Y-%m-%d %H:%M:%S'), 'author': comment_item.uid, 'author_name': author.username, 'replay_id': comment_item.replay_id, 'replay_to_author': user.id if replay_to else None, 'replay_to_author_name': user.username if replay_to else None, 'is_liked': is_liked } comments.append(comment) data = {'all_comments': comments} return success(data=data, message="获取评论成功") else: return params_error("类型错误")
def index(): context = { 'questions': Question.query.order_by(db.desc(Question.create_time)).all() } allq = Question.query.order_by(db.desc(Question.create_time)).all() picids = {} for q in allq: picids[q.id] = str(q.author.picid) return render_template('index.html', usermsg=g.user, picid=str(g.user.picid), picids=picids, **context)
def detail(question_id): page = request.args.get('page', 1, type=int) pagination = Question.query.paginate(page, per_page=10, error_out=False) pagination1 = Answer.query.order_by(db.desc(Answer.create_time)).limit(5) pagination2 = Question.query.order_by(db.desc( Question.create_time)).limit(5) context = { 'questions': pagination.items, 'answers': pagination1, 'question_show': pagination2 } question_model = Question.query.filter(Question.id == question_id).first() return render_template('detail.html', question=question_model, **context)
def index(): page = request.args.get('page', 1, type=int) pagination = Question.query.paginate(page, per_page=10, error_out=False) pagination1 = Answer.query.order_by(db.desc(Answer.create_time)).limit(5) pagination2 = Question.query.order_by(db.desc( Question.create_time)).limit(5) context = { 'questions': pagination.items, 'answers': pagination1, 'question_show': pagination2 } return render_template('index.html', pagination=pagination, **context)
def get_article(): page_size = int(request.values.get('page_size', 10)) page_index = int(request.values.get('page_index', 1)) sort = request.values.get('sort') title = request.values.get('title') content = request.values.get('content') article_type = request.values.get('type') try: query_article = Articles.query if sort: query_article = query_article.order_by( db.desc(Articles.create_time)) if title: query_article = query_article.filter( Articles.title.like('%{}%'.format(title))) if content: query_article = query_article.filter( Articles.content.like('%{}%'.format(content))) if article_type: query_article = query_article.filter( Articles.type.like('%{}%'.format(article_type))) paginate = query_article.paginate(page_index, page_size, False) data = { "total": len(paginate.items), "data": [class_to_dict(item) for item in paginate.items] } return jsonify(get_result("SUCCESS", data)) except: db.session.rollback() logger.info(traceback.extract_stack()) return jsonify(get_result("DB_ERROR", {}))
def search(): q = request.args.get('q') questions = Question.query.filter( or_(Question.title.contains(q), Question.content.contains(q))).order_by( db.desc(Question.create_time)) return render_template('index.html', questions=questions)
def get_articles(): condition = request.values.get("condition", None) sort = request.values.get("sort", None) page_index = int(request.values.get("page_index", 1)) page_size = int(request.values.get("page_size", 10)) query_result = Articles.query data = [] try: if condition: query_result = query_result.filter(Articles.title.like('%{}%'.format(condition)) | Articles.content.like('%{}%'.format(condition)) | Articles.type.like('%{}%'.format(condition))) if sort: pass else: query_result = query_result.order_by(db.desc(Articles.create_time)) paginate = query_result.paginate(page_index, page_size) data = { 'total': paginate.total, 'data': [class_to_dict(item) for item in paginate.items] } results = jsonify(get_result('SUCCESS', data)) except: print traceback.format_exc() results = jsonify(get_result('DB_ERROR', data)) return results
def list_video_color(): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) video = [] all_video = db.session.query(Video).filter(Video.id).filter( Video.type == 2).order_by(db.desc(Video.upload_time)).all() length = len(all_video) if length > 50: candidate_video = all_video[0:50] else: candidate_video = all_video[0:length] i = 1 length = len(candidate_video) candidate_video.sort(key=lambda Video: (len(list(map(int, Video.views.split(',')))) if Video.views else 0) * 4 + Video.coins * 2 + Video. comments * 2 + Video.collections * 2, reverse=True) if length < 8: for video_item in candidate_video: video.append(video_item.id) while i < 9 and length > 8: video_position = random.randint(0, length - 1) if candidate_video[video_position].id in video: continue else: video.append(candidate_video[video_position].id) i += 1 data = {'video_list': video} return success(data=data, message="获取视频成功")
def get_video(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) all_video = db.session.query(Video).filter_by(uid=id_).order_by( db.desc(Video.upload_time)).all() video_list = [] if all_video: for video in all_video: video_item = { 'pv': video.id, 'title': video.title, 'type': video.type, 'duration': video.duration, 'likes': len(list(map(int, video.likes_user.split(',')))) if video.likes_user else 0, 'views': len(list(map(int, video.views.split(',')))) if video.views else 0, 'danmuku': video.danmuku, 'time': video.upload_time.strftime('%Y-%m-%d %H:%M:%S'), 'bucket_cover': get_bucket_token(video.cover) } video_list.append(video_item) data = {'video_list': video_list} return success(data=data, message="获取投稿成功")
def search(): q = request.args.get('q') questions=Question.query.filter(or_(Question.title.contains(q), Question.content.contains(q))).order_by(db.desc(Question.create_time)) picids = {} for q in questions: picids[q.id] = str(q.author.picid) return render_template('index.html',questions=questions,usermsg=g.user,picids=picids,picid=str(g.user.picid))
def classify(): try: context = { 'questions': Question.query.order_by(db.desc(Question.create_time)).all() } return render_template('classify.html',**context) except: return render_template('404.html')
def upload(): # print(request.args.get('page')) page = int(request.args.get('page', 1)) per_page = int(request.args.get('per_page', 2)) paginate = Question.query.order_by(db.desc(Question.create_time)).paginate( page, per_page, error_out=False) questions = paginate.items question_max_id = Question.query.order_by(db.desc(Question.id)).first().id context = { 'paginate': paginate, 'questions': questions, 'question_max_id': question_max_id } return render_template('upload.html', **context) # **的使用
def search(): q = request.form.get('q') print(q) #或 question = Question.query.filter( or_(Question.content.contains(q), Question.title.contains(q))).order_by(db.desc('create_time')) print(question) return render_template('index.html', question=question)
def index(): if 'user_id' in session.keys(): user = User.query.filter(User.id == session.get('user_id')).first() gender = User.query.filter(User.sex != user.sex).all() gender_id = [] for id in gender: gender_id.append(id.id) gender_id.append(user.id) print(gender_id) context = { 'life': Life.query.filter(Life.author_id.in_(gender_id)).order_by( db.desc('creat_time')).all() } return render_template("index.html", **context) else: context = {'life': Life.query.order_by(db.desc('creat_time')).all()} return render_template("index.html", **context)
def index(): articles = Article.query.order_by(db.desc(Article.id)).all() # categories = Category.query.filter(Category.leave > 9).order_by(db.desc(Category.id)).all() categories = Category.query.filter(Category.leave > 9).all() for cate in categories: id = cate.id return render_template('index.html', articles=articles, categories=categories, id=id)
def search(request): search_str = request.args.get("q") if search_str: questions = Question.query.filter( or_(Question.content.contains(search_str), Question.title.contains(search_str))).order_by( db.desc("create_time")).all() if questions: return questions return "nf"
def index(): page = int(request.args.get('page',1)) per_page = int(request.args.get('per_page',2)) paginate = Video.query.order_by(db.desc(Video.create_time)).paginate(page,per_page,error_out=False) videos = paginate.items user_id = session.get('user_id') user = User.query.filter(User.id==user_id).first() user_role = 'pup' if user: user_role = user.role return render_template('index.html',paginate=paginate,videos=videos,user_role=user_role)
def saleOrder(): # 判断用户是否登录 user_id = session.get('user_id') if user_id: user = User.query.filter(User.id == user_id).first() if user: drugs = Drug.query.filter(Drug.saleCount != 0).order_by( db.desc(Drug.saleCount)).all() return render_template('saleOrder.html', drugs=drugs) return redirect(url_for('login'))
def list_video_related(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) video_list = [] video = db.session.query(Video).filter(Video.id == id_).first() all_video = db.session.query(Video).filter(Video.uid == video.uid).all() length = len(all_video) if length > 8: candidate_video = all_video[0:9] else: candidate_video = all_video[0:length] if video in candidate_video: candidate_video.remove(video) all_video = db.session.query(Video).filter(Video.id).filter( Video.type == 1).filter(Video.uid != video.uid).order_by( db.desc(Video.upload_time)).all() length = 8 - len(candidate_video) if len(all_video) <= 16 + length: video_temp = all_video[0:8 + length] for video_item in video_temp: candidate_video.append(video_item) else: i = 1 while i < length + 9 and i < len(all_video) + length: video_position = random.randint(0, length - 1) if all_video[video_position] in candidate_video: continue else: candidate_video.append(all_video[video_position]) i += 1 for video_item in candidate_video: user = db.session.query(User).filter_by(id=video_item.uid).first() video = { 'id': video_item.id, 'title': video_item.title, 'views': len(list(map(int, video_item.views.split(',')))) if video_item.views else 0, 'danmuku': video_item.danmuku, 'duration': video_item.duration, 'author_name': user.username, 'bucket_cover': get_bucket_token(video_item.cover) } video_list.append(video) data = {'video_list': video_list} return success(data=data, message="获取视频成功")
def index(): form = BuyBookForm() searchform = SearchForm() books = Book.query.filter(Book.state == "正在卖").order_by(db.desc( Book.id)).all() return render_template('index.html', books=books, base64=base64, str=str, form=form, searchform=searchform)
def get_com_info(): questions = Question.query.order_by(db.desc("create_time")).all() count = db.session.query( Comment.question_id, func.count(Comment.question_id)).select_from(Comment).group_by( Comment.question_id).all() print(count) for item in questions: for n in count: if item.id == n[0]: item.count = n[1] return questions
def detail(video_id): user_id = session.get('user_id') user = User.query.filter(User.id == user_id).first() user_role = 'pup' if user: user_role = user.role page = int(request.args.get('page',1)) per_page = int(request.args.get('per_page',2)) video = Video.query.filter(Video.id == video_id).first() paginate = Comment.query.filter(Comment.video_id==video_id).order_by(db.desc(Comment.create_time)).paginate(page,per_page,error_out=False) comments = paginate.items return render_template('detail.html', video=video,paginate=paginate,user_role=user_role, comments=comments)
def searchlabel(name): try: q = name.decode('utf8') #print q questions=Question.query.filter(or_(Question.title.contains(q), Question.content.contains(q))).order_by(db.desc(Question.create_time)) label1 = q picids = {} for q in questions: picids[q.id] = str(q.author.picid) return render_template('search.html',questions=questions,usermsg=g.user,picids=picids,picid=str(g.user.picid)) except: return render_template('404.html')
def index(): # context = { # 'questions': Question.query.order_by(db.desc('create_time')).all() # } questions = Question.query.order_by(db.desc('create_time')).all() # return render_template("index.html", **context) pager_obj = Pagination(request.args.get("page", 1), len(questions), request.path, request.args, per_page_count=10) questions = questions[pager_obj.start:pager_obj.end] html = pager_obj.page_html() return render_template("index.html", questions=questions, html=html)
def hotsort(): form = BuyBookForm() searchform = SearchForm hotsortform = HotSortForm() books = Book.query.filter(Book.state == "正在卖").all() if hotsortform.validate_on_submit(): books = Book.query.order_by(db.desc(Book.sales)).all() return render_template('index.html', books=books, base64=base64, str=str, form=form, searchform=searchform, hotsortform=hotsortform)
def index_page(): all_articles = [] # 显示最新三篇文章 articles = Articles.query.order_by(db.desc(Articles.create_time)) articles = articles.all() if articles: article_num = (len(articles) if config.index_article_num >= len(articles) else config.index_article_num) for i in range(article_num): author = articles[i].author.username articles[i] = class_to_dict(articles[i]) articles[i]['author'] = author articles[i]['content'] = articles[i]['content'][0:40] + '......' all_articles.append(articles[i]) return render_template('index.html', articles=all_articles)
def addStocHistory(): # 判断用户是否登录 user_id = session.get('user_id') if user_id: user = User.query.filter(User.id == user_id).first() if user: drugs = [] drugsfromDb = db.session.query( Drug.num, Drug.name, Drug.stockDate, func.count('*').label('count')).group_by( Drug.stockDate).order_by(db.desc(Drug.stockDate)) # 从数据库查到列表 for drug in drugsfromDb: drugs.append(drug) return render_template('addStockHis.html', drugs=drugs) return redirect(url_for('login'))
def leave_message(): form = MessageForm() messages = Message.query.order_by(db.desc(Message.id)).all() if form.validate_on_submit(): user_id = 0 username = "******" if current_user.is_authenticated: user_id = current_user.id username = current_user.username message = Message(user_id=user_id, username=username, text=form.text.data) db.session.add(message) db.session.commit() flash('留言成功!') return redirect(url_for('leave_message')) return render_template('leave_message.html', form=form, messages=messages)