首先我们要完成以下页面的展示
以下是前端代码
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>首页-新经资讯</title> 6 <link rel="stylesheet" type="text/css" href="../../static/news/css/reset.css"> 7 <link rel="stylesheet" type="text/css" href="../../static/news/css/main.css"> 8 <script type="text/javascript" src="../../static/news/js/jquery-1.12.4.min.js"></script> 9 <script type="text/javascript" src="../../static/news/js/main.js"></script> 10 <script type="text/javascript" src="../../static/news/js/index.js"></script> 11 </head> 12 <body> 13 <div class="header_con"> 14 <div class="header"> 15 <a href="#" class="logo fl"> 16 <img src="../../static/news/images/logo.png" alt="logo"></a> 17 <ul class="menu fl"> 18 {# #for循环遍历所有的新闻分类#} 19 {% for foo in context.categories %} 20 {# #loop循环迭代计数(从1开始),这里是如果等于1的话,高亮显示#} 21 {% if loop.index == 1 %} 22 {# #显示新闻分类(高亮显示) #} 23 <li class="active" data-cid="{{ foo.id }}"> 24 <a href="javascript:;">{{ foo.name }}</a></li> 25 {% else %} 26 {# #显示新闻分类#} 27 <li data-cid="{{ foo.id }}"> 28 <a href="javascript:;">{{ foo.name }}</a></li> 29 {% endif %} 30 {% endfor %} 31 32 {# <li class="active" data-cid="1"><a href="javascript:;">最新</a></li>#} 33 {# <li data-cid="2"><a href="javascript:;">股市</a></li>#} 34 {# <li data-cid="3"><a href="javascript:;">债市</a></li>#} 35 {# <li data-cid="4"><a href="javascript:;">商品</a></li>#} 36 {# <li data-cid="5"><a href="javascript:;">外汇</a></li>#} 37 {# <li data-cid="6"><a href="javascript:;">公司</a></li>#} 38 </ul> 39 {% if context.user %} 40 <div class="user_login fr"> 41 <img src="{% if context.user.avatar_url %}{{ context.user.avatar_url }}{% else %}../../static/news/images/person01.png{% endif %}" class="lgin_pic"> 42 <a href="{{ url_for("user.user_info") }}">{{ context.user.nick_name }}</a> 43 {#html中的内容#} 44 <a href="javascript:;"onclick="logout()">退出</a> 45 </div> 46 47 {% else %} 48 <div class="user_btns fr"> 49 <a href="javascript:;" class="login_btn">登录</a> / <a href="javascript:;" class="register_btn">注册</a> 50 </div> 51 {% endif %} 52 53 54 55 {# <!-- 用户登录后显示下面,隐藏上面 -->#} 56 57 58 59 </div> 60 </div> 61 62 <div class="conter_con"> 63 <ul class="list_con fl"> 64 65 </ul> 66 <div class="rank_con fr"> 67 <div class="rank_title"> 68 <h3>点击排行</h3> 69 </div> 70 <ul class="rank_list"> 71 {% for new in context.click_news %} 72 <li><span class="{{ loop.index|rank }}">{{ loop.index }}</span> 73 <a href="#">{{ new.title }}</a></li> 74 {% endfor %} 75 </ul> 76 </div> 77 </div> 78 <div class="footer"> 79 <div class="footer_links"> 80 <a href="">关于我们</a> 81 <span>|</span> 82 <a href="">联系我们</a> 83 <span>|</span> 84 <a href="">招聘人才</a> 85 <span>|</span> 86 <a href="">友情链接</a> 87 </div> 88 <p class="copyright"> 89 CopyRight © 2018 新经资讯信息技术有限公司 All Rights Reserved<br /> 90 电话:010-****888 京ICP备*******8号 91 </p> 92 </div> 93 94 <!-- 登录表单 --> 95 <form class="login_form_con"> 96 <div class="login_form"> 97 <div class="login_title"> 98 <h3>登 录</h3> 99 <a href="javascript:;" class="shutoff"></a> 100 </div> 101 <div class="form_group"> 102 <input id="mobile" type="text" name="mobile" autocomplete="off"> 103 <div class="input_tip">手机号</div> 104 <div id="login-mobile-err" class="error_tip">手机号不能为空</div> 105 </div> 106 <div class="form_group"> 107 <input id="password" type="password" name="password"> 108 <div class="input_tip">密码(不少于6位)</div> 109 <div id="login-password-err" class="error_tip">密码不能为空</div> 110 </div> 111 <input type="submit" name="" value="登 录" class="input_sub"> 112 <div class="down_link">还没有账号?<a href="javascript:;" class="to_register">立即注册</a></div> 113 </div> 114 <div class="mask"></div> 115 </form> 116 117 <!-- 注册表单 --> 118 <form class="register_form_con"> 119 <div class="register_form"> 120 <div class="register_title"> 121 <h3>注 册</h3> 122 <a href="javascript:;" class="shutoff"></a> 123 </div> 124 <div class="form_group"> 125 <input type="text" name="mobile" autocomplete="off" id="register_mobile" class="phone_input"> 126 <div class="input_tip">手机号</div> 127 <div id="register-mobile-err" class="error_tip">手机号不能为空</div> 128 </div> 129 <div class="form_group"> 130 <input type="text" name="code_pwd" id="imagecode" class="code_pwd"> 131 <div class="input_tip">图形验证码</div> 132 <img src="../../static/news/images/pic_code.png" class="get_pic_code" onclick="generateImageCode()"> 133 <div id="register-image-code-err" class="error_tip">图形码不能为空</div> 134 </div> 135 <div class="form_group"> 136 <input type="text" name="smscode" id="smscode" class="code_pwd"> 137 <div class="input_tip">手机验证码</div> 138 <a href="javascript:;" class="get_code" onclick="sendSMSCode()">点击获取验证码</a> 139 <div id="register-sms-code-err" class="error_tip">验证码不能为空</div> 140 </div> 141 <div class="form_group"> 142 <input type="password" name="password" id="register_password" class="pass_input"> 143 <div class="input_tip">密码(不少于6位)</div> 144 <div id="register-password-err" class="error_tip">密码不能为空</div> 145 </div> 146 <div class="form_group2 clearfix"> 147 <input type="checkbox" class="agree_input" checked> 148 <p>同意使用条款,并已阅读"跟帖评论自律管理承诺书"</p> 149 <div class="error_tip">请勾选</div> 150 </div> 151 <input type="submit" name="" value="注 册" class="input_sub"> 152 <div class="down_link">已有账号?<a href="javascript:;" class="to_login">立即登录</a></div> 153 </div> 154 <div class="mask"></div> 155 </form> 156 </body> 157 </html>
以下是后端的代码
1 from . import index_blue 2 from info import redis_store 3 from flask import render_template,current_app,session,request,jsonify 4 from info.models import User,News,Category 5 from info.response_code import RET 6 @index_blue.route('/') 7 def index(): 8 user_id = session.get('id') 9 user = None 10 try: 11 user = User.query.filter_by(id=user_id).first() 12 except Exception as e: 13 current_app.logger.error(e) 14 15 click_news = None 16 try: 17 click_news = News.query.order_by(News.clicks.desc()).limit(6) 18 except Exception as e: 19 current_app.logger.error(e) 20 21 categories = None 22 23 try: 24 categories = Category.query.all() 25 except Exception as e: 26 current_app.logger.error(e) 27 28 context = { 29 "user": user, 30 "click_news": click_news, 31 "categories" :categories, 32 } 33 return render_template("news/index.html", context=context)