功能需求:
中间展示新闻分类信息
右侧显示新闻点击排行
根据分类进行新闻列表展示
上拉加载更多数据
点击新窗口跳转到新闻详情页
重点功能分析
因为新闻列表页需要实现上拉加载更多的交互逻辑
在用户上拉之后,新增的新闻数据直接拼接到页面的最下方,无需要更新整个页面
所以新闻数据的展示不能采用渲染模板的形式
需要使用 ajax 请求,请求完毕之后拼接界面元素
点击排行(功能实现)
modules目录,index目录views.py,index函数添加如下代码
news_clicks = []
try:
news_clicks = News.query.order_by(News.clicks.desc()).limit(6)
except Exception as e:
current_app.logger.error(e)
abort(500)
index.html,(在class值为‘rank_list’)进行如下修改,将原先的li标签注释掉
{% for news in context.news_clicks %}
<li><span class="{{ loop.index | rank }}">{{ loop.index }}</span><a href="#">{{ news.title }}</a></li>
{% endfor %}
新闻分类(功能实现)
modules目录,index目录views.py,index函数添加如下代码
category = []
try:
category = Category.query.all()
except Exception as e:
current_app.logger.error(e)
abort(500)
将 category 添加至我们的context中
context = {
"user": user,
"news_clicks": news_clicks,
"category":category
}
index.html,(在class值为‘menu fl’)进行如下修改,将原先的li标签注释掉
{% for category in context.category %}
<li class="{% if loop.index0 == 0 %}active{% endif %}" data-cid="{{ category.id }}"><a href="javascript:;">{{ category.name }}</a></li>
{% endfor %}
新闻列表数据(功能实现)
接口设计
代码实现
modules目录,index目录views.py,新建一个视图函数“news_list”
@index_blue.route('/news_list')
def news_list():
"""
新闻获取
1.参数获取
2.校验参数
2.1 校验是否齐全
2.2 校验page和per_page是否为数值型
3.完成新闻的获取(分页 )
4.返回json数据
:return:
"""
cid = request.args.get('cid')
page = request.args.get('page',1)
per_page = request.args.get('per_page',constants.HOME_PAGE_MAX_NEWS )
if not cid:
return jsonify(errno=RET.PARAMERR, errmsg='缺失参数')
try:
cid = int(cid)
page = int(page)
per_page = int(per_page)
except Exception as e:
current_app.logger.error(e)
cid = 1
page = 1
per_page = constants.HOME_PAGE_MAX_NEWS
try:
if cid == 1:
paginate = News.query.order_by(News.create_time.desc()).paginate(page,per_page)
else:
paginate = News.query.filter_by(category_id=cid).order_by(News.create_time.desc()).paginate(page,per_page)
except Exception as e:
current_app.logger.error(e)
return jsonify(errno=RET.DBERR, errmsg='新闻获取失败')
total_page = paginate.pages
currentPage = paginate.page
newsList = paginate.items
newsList_dict = []
for news in newsList:
newsList_dict.append(news.to_basic_dict())
data = {
"total_page":total_page,
"currentPage":currentPage,
"news_dict_list":newsList_dict
}
return jsonify(errno=RET.OK, errmsg='新闻获取成功', data=data)
进入index.html,找到js引入的‘index.js’,ctrl+左键进入,进行如下修改
var currentCid = 1;
var cur_page = 1;
var total_page = 1;
var data_querying = true;
$(function () {
updateNewsData();
$('.menu li').click(function () {
var clickCid = $(this).attr('data-cid')
$('.menu li').each(function () {
$(this).removeClass('active')
})
$(this).addClass('active')
if (clickCid != currentCid) {
currentCid = clickCid;
cur_page = 1;
total_page = 1;
updateNewsData()
}
});
$(window).scroll(function () {
var showHeight = $(window).height();
var pageHeight = $(document).height();
var canScrollHeight = pageHeight - showHeight;
var nowScroll = $(document).scrollTop();
if ((canScrollHeight - nowScroll) < 100) {
if (!data_querying) {
data_querying = true;
cur_page += 1;
if (cur_page < total_page) {
updateNewsData();
}
}
}
})
});
function updateNewsData() {
var params = {
'cid':currentCid,
'page':cur_page
};
$.get('/news_list', params, function (response) {
data_querying = false;
if (response.errno == '0') {
total_page = response.data.total_page;
if (cur_page == 1) {
$(".list_con").html("");
}
for (var i=0;i<response.data.news_dict_list.length;i++) {
var news = response.data.news_dict_list[i]
var content = '<li>'
content += '<a href="/news/detail/'+news.id+'" class="news_pic fl"><img src="' + news.index_image_url + '?imageView2/1/w/170/h/170"></a>'
content += '<a href="/news/detail/'+news.id+'" class="news_title fl">' + news.title + '</a>'
content += '<a href="/news/detail/'+news.id+'" class="news_detail fl">' + news.digest + '</a>'
content += '<div class="author_info fl">'
content += '<div class="source fl">来源:' + news.source + '</div>'
content += '<div class="time fl">' + news.create_time + '</div>'
content += '</div>'
content += '</li>'
$(".list_con").append(content);
}
} else {
alert(response.errmsg);
}
});
}
效果展示
|