1.1新聞首頁的顯示
阿新 • • 發佈:2020-11-02
首先我們要完成以下頁面的展示
以下是前端程式碼
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)