6hz7vif 发表于 2024-10-10 05:20:55

Django研发网站-5-运用bootstrap模板美化前端页面


    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/30a8322702524dfeb8c3cd103339e471~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728744684&amp;x-signature=mEKbcQ7%2FNAuy9t55quY1Q%2FoU83E%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">首要</span><span style="color: black;">咱们</span>打开bootstrap的网站,查看bootstarp的中文文档</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">简介 · Bootstrap v4 中文文档 v4.6 | Bootstrap 中文网 (bootcss.com)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">中文文档中有对bootstrap的<span style="color: black;">仔细</span>介绍。<span style="color: black;">能够</span>在里面逐个学习。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>今天用bootstrap美化页面,<span style="color: black;">表示</span>上篇<span style="color: black;">文案</span>中的内容,并实现<span style="color: black;">查找</span>功能。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在快速入门中,<span style="color: black;">咱们</span>找到入门模板,将里面的代码复制。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span>再users的templates中新建一个show2.html文件,将入门模板粘贴到show2.html中。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p9-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/f95cfb6c8a274329985507b7b92f2b00~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728744684&amp;x-signature=hL1I%2FpO7owVFCUYSzlghFkz128U%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在views.py中<span style="color: black;">增多</span>一个函数show_excel2.html,</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/eb7c3be9149c47a28a78cf4377e4b21f~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728744684&amp;x-signature=AqIItQ44NlRxiuq3warZmMjHYjk%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在url.py中<span style="color: black;">增多</span>路由,</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/c6981558e7bc4e11b246f294c7f8b73a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728744684&amp;x-signature=nHaTUmEKZe6ikBSXfNnM%2BLKT2Ks%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">运行程序,在浏览器中输入</p>http://127.0.0.1:8000/show_excel2,页面正常<span style="color: black;">表示</span>,说明路由、页面均是正常。下面在show2.html中<span style="color: black;">运用</span>bootstrap的表格样式。

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">打开bootstrap中文文档,左侧<span style="color: black;">选取</span>页面内容——表格,在example中<span style="color: black;">选取</span>第二个例子,复制粘贴到show_excel2.html中。在对样例进行修改,代码如下:</span></p>&lt;div <span style="color: black;"><span style="color: black;">class</span></span>=<span style="color: black;">"container"</span>&gt;<span style="color: black;"><span style="color: black;">&lt;!--做一个简单的布局--&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"row"</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">table</span> <span style="color: black;">class</span>=<span style="color: black;">"table table-dark"</span>&gt;</span><span style="color: black;">&lt;!--<span style="color: black;">表示</span>数据的表格--&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">thead</span>&gt;</span><span style="color: black;">&lt;!--设置表头部分--&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">tr</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">th</span> <span style="color: black;">scope</span>=<span style="color: black;">"col"</span>&gt;</span>学号<span style="color: black;">&lt;/<span style="color: black;">th</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">th</span> <span style="color: black;">scope</span>=<span style="color: black;">"col"</span>&gt;</span>姓名<span style="color: black;">&lt;/<span style="color: black;">th</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">th</span> <span style="color: black;">scope</span>=<span style="color: black;">"col"</span>&gt;</span>语文<span style="color: black;">&lt;/<span style="color: black;">th</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">th</span> <span style="color: black;">scope</span>=<span style="color: black;">"col"</span>&gt;</span>数学<span style="color: black;">&lt;/<span style="color: black;">th</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">th</span> <span style="color: black;">scope</span>=<span style="color: black;">"col"</span>&gt;</span>英语<span style="color: black;">&lt;/<span style="color: black;">th</span>&gt;</span>
      <span style="color: black;">&lt;/<span style="color: black;">tr</span>&gt;</span>
      <span style="color: black;">&lt;/<span style="color: black;">thead</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">tbody</span>&gt;</span>{% for idx, row in dg.iterrows %}<span style="color: black;">&lt;!--循环语句<span style="color: black;">表示</span>数据--&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">tr</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">td</span>&gt;</span>{{ row.学号 }}<span style="color: black;">&lt;/<span style="color: black;">td</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">td</span>&gt;</span>{{ row.姓名 }}<span style="color: black;">&lt;/<span style="color: black;">td</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">td</span>&gt;</span>{{ row.语文 }}<span style="color: black;">&lt;/<span style="color: black;">td</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">td</span>&gt;</span>{{ row.数学 }}<span style="color: black;">&lt;/<span style="color: black;">td</span>&gt;</span>
      <span style="color: black;">&lt;<span style="color: black;">td</span>&gt;</span>{{ row.英语 }}<span style="color: black;">&lt;/<span style="color: black;">td</span>&gt;</span>
      <span style="color: black;">&lt;/<span style="color: black;">tr</span>&gt;</span>
      {% endfor %}
      <span style="color: black;">&lt;/<span style="color: black;">tbody</span>&gt;</span>
      <span style="color: black;">&lt;/<span style="color: black;">table</span>&gt;</span>
      <span style="color: black;">&lt;/<span style="color: black;">div</span>&gt;</span>
      <span style="color: black;">&lt;/<span style="color: black;">div</span>&gt;</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">刷新浏览器页面,<span style="color: black;">发掘</span>页面的数据<span style="color: black;">表示</span>出来,表格<span style="color: black;">出现</span>了变化。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/a6dccf36f6354c889a663d7ad40a156a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728744684&amp;x-signature=vMw1mh3iSG1Le7yuiO3QJtw6IbM%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下面<span style="color: black;">增多</span>一个<span style="color: black;">查找</span>功能。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在show_excel2.html页面中的</span><span style="color: black;">&lt;div class="container"&gt;&lt;/div&gt;</span>内的表格上方<span style="color: black;">增多</span>一个<span style="color: black;">&lt;div class="row"&gt;&lt;/div&gt;</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">打开bootstrap中文文档,左侧<span style="color: black;">选取</span>组件——表单,复制粘贴到页面中。修改样式。代码如下:</span></p><span style="color: black;">&lt;<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"container"</span>&gt;</span><span style="color: black;">&lt;!--做一个简单的布局--&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"row"</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">form</span> <span style="color: black;">class</span>=<span style="color: black;">"form-inline"</span> <span style="color: black;">method</span>=<span style="color: black;">"post"</span>&gt;</span>
    {% csrf_token %} <span style="color: black;">&lt;!--设置一个token令牌,这是Django的<span style="color: black;">守护</span>机制,<span style="color: black;">无</span>这个参数,会报错--&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"form-group mb-2"</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">label</span> <span style="color: black;">for</span>=<span style="color: black;">"staticEmail2"</span> &gt;</span>成绩<span style="color: black;">查找</span><span style="color: black;">&lt;/<span style="color: black;">label</span>&gt;</span>
    <span style="color: black;">&lt;/<span style="color: black;">div</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"form-group mx-sm-3 mb-2"</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">input</span> <span style="color: black;">type</span>=<span style="color: black;">"text"</span> <span style="color: black;">class</span>=<span style="color: black;">"form-control"</span> <span style="color: black;">name</span>=<span style="color: black;">"keyword"</span> <span style="color: black;">id</span>=<span style="color: black;">"search1"</span> <span style="color: black;">placeholder</span>=<span style="color: black;">"请输入搜索关键词"</span>&gt;</span>
    <span style="color: black;">&lt;/<span style="color: black;">div</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">button</span> <span style="color: black;">type</span>=<span style="color: black;">"submit"</span> <span style="color: black;">class</span>=<span style="color: black;">"btn btn-primary mb-2"</span>&gt;</span><span style="color: black;">查找</span><span style="color: black;">&lt;/<span style="color: black;">button</span>&gt;</span>
    <span style="color: black;">&lt;/<span style="color: black;">form</span>&gt;</span>
    <span style="color: black;">&lt;/<span style="color: black;">div</span>&gt;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在修改views.py代码,实现<span style="color: black;">查找</span>功能。</span></p><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">show_excel2</span><span style="color: black;">(request)</span>:</span>
    df=pd.read_excel(BASE_DIR/<span style="color: black;">data/1.xlsx</span>) <span style="color: black;">#读取excel文件中的数据</span>
    keyword=<span style="color: black;">""</span>
    <span style="color: black;">if</span> request.method==<span style="color: black;">"POST"</span>:
    keyword=request.POST[<span style="color: black;">"keyword"</span>]
    keyword=str(keyword).strip() <span style="color: black;">#去除多余空格</span>

    <span style="color: black;">if</span> keyword:
    df=df[(df[<span style="color: black;">"姓名"</span>]==keyword) | (df[<span style="color: black;">"学号"</span>]==keyword)]

    <span style="color: black;">return</span>render(request,<span style="color: black;">"show2.html"</span>,{<span style="color: black;">dg</span>:df})<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运行程序。在搜索框中输入学号 或姓名,点击搜索,<span style="color: black;">表示</span>搜索结果。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/6957d25716164a0d8cb09261945a6c6f~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728744684&amp;x-signature=JcjQ8o2gKfpBYQBguNzNFakFQfY%3D" style="width: 50%; margin-bottom: 20px;"></div>




页: [1]
查看完整版本: Django研发网站-5-运用bootstrap模板美化前端页面