实训9:长征概述页其它模块的实现【附完整代码】

在本实训中,将继续完善长征概述页的相关内容,为长征概述页添加“长征•行军路线”、“长征•重要会议”、“长征•重要将领”和“长征•名称由来”四个模块。每个模块的功能与已经实现的“长征•历史背景”模块类似,分别从不同角度概要描述有关红军长征的相关信息,并提供有跳转到对应的详细分类页面或其它模块的链接。因此各模块的实现方式与“长征•历史背景”模块类似,首先修改cz_profile.py中的视图函数profile_page()的代码,利用json模块的load()方法从cz_profile_data.json文件加载相关数据,然后通过render_template()方法将数据传递给模板文件,最后在模板文件中接收数据,并将数据渲染到模板的合适位置。具体步骤如下:

  1. 1、修改cz_profile.py (位于项目根目录下的views/子目录内),将原来在实训8中实现的profile_page()函数中利用文件和字符串相关操作提取数据的代码注释或删除,然后添加基于json模块加载数据的代码,最后将加载的数据传递给模板文件,如图1所示。
  2. 图1 profile_page()函数基于json模块加载数据
    import json
    import os
    from flask import Blueprint, render_template
    from models.global_data import v_list
    profile = Blueprint('profile', __name__)
    
    
    @profile.route('/profile')
    def profile_page():
        fpath = os.path.join(os.getcwd(), "static/jsons/cz_profile_data.json")  # 构造示例数据文件的路径字符串
        # 用open方法,根据示例数据文件路径打开json文件,并用json模块的Load方法将数据加载到相应变量中
        with open(fpath, mode='r', encoding='utf-8') as fp:
            profiles = json.load(fp=fp)
            lines = []
            if profiles:
                # 将长征中的红军队伍相关数据组织到一个列表中,以便于html页面中使用
                for idx, item in enumerate(profiles["czProfileArmy"]):
                    temp = item.split(":", 1)
                    line = {
                        'army': temp[0],
                        'desc': temp[1],
                        'line_img': profiles["czProfileLinePics"][idx]
                    }
                    idx += 1
                    lines.append(line)
    
        # 利用render_template方法,将相关数据渲染到模板文件中
        return render_template("index/cz_profile.html", v_list=v_list, profiles=profiles, lines=lines)

  3. 2、修改cz_profile.html (位于项目根目录下的templates/index子目录下), 与“长征•历史背景”模块的实现类似,“长征•行军路线”模块的全部代码(位于一对<section></section>标签内),应该位于“长征•历史背景”模块的结束标签</section>后面,main块的结束标志{% endblock %}前面的代码区间内,如图2所示,页面的运行效果如图3所示。
  4. 图2 cz_profile.html模板文件中“长征•行军路线”模块的代码
  5. 图3 “长征•行军路线”模块的页面呈现效果3、继续修改cz_profile.html (位于项目根目录下的templates/index子目录下), 与“长征•行军路线”模块类似实现长征概述页的其余模块,相关的模块代码及页面呈现效果如图4---图9所示。图4 cz_profile.html模板文件中“长征•重要会议”模块的代码图5 “长征•重要会议”模块的页面呈现效果图6 cz_profile.html模板文件中“长征•重要将领”模块的代码图7 “长征•重要将领”模块的页面呈现效果图8 cz_profile.html模板文件中“长征•名称由来”模块的代码图9 “长征•名称由来”模块的页面呈现效果
    {% extends "/cz_base.html" %}  
    {% block main %}
    <!-- 长征·图片轮播 -->
    <section id="myCarousel" class="carousel slide" data-bs-ride="carousel">  
        <ul class="carousel-indicators">
          {% for v in v_list %}
          {% if loop.first %}
          <li data-bs-target="#myCarousel" data-bs-slide-to="{{loop.index0}}"
              class="active" aria-current="true" aria-label="Slide{{loop.index}}"></li>
          {% else %}
          <li data-bs-target="#myCarousel" data-bs-slide-to="{{loop.index0}}"
              aria-label="Slide{{loop.index}}"></li>
          {% endif %}
          {% endfor %}
        </ul>
        <div class="carousel-inner">
          {% for v in v_list %}
          {% if loop.first %}
          <div class="carousel-item active">
            <img class="d-block w-100" src="{{ url_for('static',filename=v.img_src) }}">
          </div>
          {% else %}
          <div class="carousel-item">
            <img class="d-block w-100" src="{{ url_for('static', filename=v.img_src) }}">
          </div>
          {% endif %}
          {% endfor %}
        </div>
        <ul class="m-0">
            <li class="carousel-control-prev" data-bs-target="#myCarousel" data-bs-slide="prev">
                <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            </li>
            <li class="carousel-control-next" data-bs-target="#myCarousel" data-bs-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
            </li>
        </ul>
    </section>  
    <!-- 长征·历史背景 -->
    <section class="mt-1">
        <div class="container pt-4">
            <div class="row">
                <div class="block-title-w">
                    <h2 class="block-titLe">长征·历史背景</h2>
                    <span class="icon-title">
                    <span></span>
                    <img src="{{ url_for('static', filename='images/star.png') }}" width="30" height="30">
                    </span>
                    <img src="{{ url_for('static', filename='images/cz_bar.jpg') }}" class="rounded w-100 mt-3">
                    <div class="text-start">
                    <span class="text-danger fw-bold fs-4 mt-3">
                     <img src="{{ url_for('static', filename='images/cz.jpg') }}" class="rounded" height="26">
                     {{ profiles.czProfileSubTitle1 }}</span>
                     <span class="sub-title fw-bold fs-4 mt-3 text-primary">
                       {{ profiles.czProfileSubTitle2 }}</span>
                     <span class="text-warning sub-title fw-semibold fs-4 mt-3">
                       {{ profiles.czProfileDescription }}</span>
                     {% for intro in profiles.czProfileIntroduction %}
                     <span class="sub-title {{ loop.cycle('bg-warning', 'bg-light') }} fw-medium fs-5 mt-3 p-3 rounded-4">
                       {{ intro }}</span>
                     {% endfor %}
                     <h2 class="block-titLe">参考资料</h2>
                     {% for ref in profiles.czProfileReferences %}
                     <span class="sub-title bg-light fw-medium fs-5 p-3 rounded-4 mb-1">{{ ref }}</span>
                     {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    </section>
    
    
    <!--长征·行军路线-->
    <section class="mt-1 py-4 bg-cz">
        <div class="container">
            <div class="block-title-w">
                <h2 class="block-title">长征·行军路线</h2>
                    <span class="icon-title">
                        <span></span>
                        <img src="{{ url_for('static', filename='images/star.png') }}" width="30" height="30">
                        </span>
            </div>
            <div class="row mb-5">
                <div class="col-md-6 col-sm-6">
                    <img class="img-fluid w-100 rounded-3"
                         src="{{ url_for('static', filename=profiles.czProfileLinePic1) }}" alt="长征路线图"/>
                </div>
                <div class="col-md-6 col-sm-6">
                    <h2 class="bLock-title fw-bolder fs-3 mb-4">基本路线</h2>
                    <span class="sub-title fw-bold fs-4 mt-3 text-primary">{{ profiles.czProfileBaseLine }}</span>
                </div>
            </div>
            <h2 class="block-title fw-bolder fs-4 mt-5">红军各部长征路线</h2>
            <div class="row">
                {% for line in lines %}
                <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 p-1">
                    <div class="fw-semibold fs-5 mt-2 text-primary ps-2">
                        <i class="bi bi-flag-fill" style="font-size:1rem;color:red;"></i>
                        {{ line.army }}
                    </div>
                    <img class="img-fluid w-100 rounded-3" src="{{ url_for('static', filename=line.line_img) }}"/>
                    <div class="cz-loss-decs">
                        <p>{{ line.desc }}</p>
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>
    </section>
    
    <!--长征·重要会议-->
    <section class="mt-1 bg-army">
        <div class="container py-4">
            <div class="row">
                <div class="block-title-w">
                    <h2 class="block-title">长征·重要会议</h2>
                    <span class="icon-title">
                    <span></span>
                    <img src="{{ url_for('static', filename='images/star.png') }}" width="30" height="30">
                    </span>
                </div>
                <div class="row">
                    {% for meeting in profiles.czProfileMeetings %}
                    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 mb-2 p-1">
                        <div class="p-1 h-100 bg-light rounded-3">
                            <div class="fw-semibold fs-5 mt-2 text-danger ps-2">
                                <i class="bi bi-flag-fill" style="font-size: 1rem; color: red;"></i>
                                {{ loop.index }}.{{ meeting.meetingTitle }}
                            </div>
                            <p>{{ meeting.meetingDate }}</p>
                            <div class="cz-loss-decs">
                                <p>{{ meeting.meetingPDescription }}</p>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </section>
    
    <!-- 长征·重要将领 -->
    <section class="mt-1 bg-cz">
        <div class="container py-4">
            <div class="block-title-w">
                <h2 class="block-title">长征·重要将领</h2>
                <span class="icon-title">
                    <span></span>
                    <img src="{{ url_for('static', filename='images/star.png') }}" width="30" height="30">
                </span>
            </div>
            <div class="row row-wrap">
                {% for leader in profiles.czProfileLeaders %}
                <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 item">
                    <div class="inner mb-3">
                        <a class="image" href="#">
                            <img src="{{ url_for('static', filename=leader) }}" class="img-fluid w-100" alt="长征将领">
                        </a>
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>
    </section>
    
    <!-- 长征·名称由来 -->
    <section class="mt-1 bg-cz">
        <div class="container">
            <div class="row">
                <div class="block-title-w">
                    <h2 class="block-title">长征·名称由来</h2>
                    <span class="icon-title">
                    <span></span>
                    <img src="{{ url_for('static', filename='images/star.png') }}" width="30" height="30">
                    </span>
                    <div class="text-start fw-semibold">
                        {% for intro in profiles.czProfileOriginal %}
                        <span class="sub-title {{ loop.cycle('bg-warning', 'bg-light') }} fs-5 mt-3 p-3 rounded-4">
                            {{ intro }}
                        </span>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    </section>
    
    {% endblock %}

  • 20
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
抱歉,我并不能为您提供完整的HTML、CSS和Vue代码,因为这需要根据您的具体需求和设计来编写。但是,我可以为您提供一些基本的代码示例来帮助您入门。 HTML示例: ```html <!DOCTYPE html> <html> <head> <title>长征</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> </head> <body> <div id="app"> <header> <nav> <ul> <li><a href="#">首</a></li> <li><a href="#">关于我们</a></li> <li><a href="#">产品</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> </header> <main> <h1>欢迎来到长征</h1> <p>这是一个基于Vue开发的网</p> <img src="logo.png" alt="长征logo"> </main> <footer> <p>版权所有 © 2021 长征</p> </footer> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="app.js"></script> </body> </html> ``` CSS示例: ```css body { margin: 0; padding: 0; font-family: Arial, sans-serif; background-color: #f8f8f8; } header { background-color: #333; color: #fff; padding: 10px; } nav ul { list-style-type: none; margin: 0; padding: 0; } nav li { display: inline-block; margin-right: 20px; } nav li:last-child { margin-right: 0; } nav a { color: #fff; text-decoration: none; } main { padding: 20px; } h1 { font-size: 36px; margin-bottom: 20px; } p { font-size: 18px; line-height: 1.5; margin-bottom: 20px; } img { max-width: 100%; height: auto; } footer { background-color: #333; color: #fff; padding: 10px; text-align: center; } ``` Vue示例: ```javascript var app = new Vue({ el: '#app', data: { message: '欢迎来到长征' } }); ``` 这些示例仅供参考,您可以根据自己的需求和想法进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值