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&from=article.pc_detail&lk3s=953192f4&x-expires=1728744684&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&from=article.pc_detail&lk3s=953192f4&x-expires=1728744684&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&from=article.pc_detail&lk3s=953192f4&x-expires=1728744684&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&from=article.pc_detail&lk3s=953192f4&x-expires=1728744684&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><div <span style="color: black;"><span style="color: black;">class</span></span>=<span style="color: black;">"container"</span>><span style="color: black;"><span style="color: black;"><!--做一个简单的布局--></span>
<span style="color: black;"><<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"row"</span>></span>
<span style="color: black;"><<span style="color: black;">table</span> <span style="color: black;">class</span>=<span style="color: black;">"table table-dark"</span>></span><span style="color: black;"><!--<span style="color: black;">表示</span>数据的表格--></span>
<span style="color: black;"><<span style="color: black;">thead</span>></span><span style="color: black;"><!--设置表头部分--></span>
<span style="color: black;"><<span style="color: black;">tr</span>></span>
<span style="color: black;"><<span style="color: black;">th</span> <span style="color: black;">scope</span>=<span style="color: black;">"col"</span>></span>学号<span style="color: black;"></<span style="color: black;">th</span>></span>
<span style="color: black;"><<span style="color: black;">th</span> <span style="color: black;">scope</span>=<span style="color: black;">"col"</span>></span>姓名<span style="color: black;"></<span style="color: black;">th</span>></span>
<span style="color: black;"><<span style="color: black;">th</span> <span style="color: black;">scope</span>=<span style="color: black;">"col"</span>></span>语文<span style="color: black;"></<span style="color: black;">th</span>></span>
<span style="color: black;"><<span style="color: black;">th</span> <span style="color: black;">scope</span>=<span style="color: black;">"col"</span>></span>数学<span style="color: black;"></<span style="color: black;">th</span>></span>
<span style="color: black;"><<span style="color: black;">th</span> <span style="color: black;">scope</span>=<span style="color: black;">"col"</span>></span>英语<span style="color: black;"></<span style="color: black;">th</span>></span>
<span style="color: black;"></<span style="color: black;">tr</span>></span>
<span style="color: black;"></<span style="color: black;">thead</span>></span>
<span style="color: black;"><<span style="color: black;">tbody</span>></span>{% for idx, row in dg.iterrows %}<span style="color: black;"><!--循环语句<span style="color: black;">表示</span>数据--></span>
<span style="color: black;"><<span style="color: black;">tr</span>></span>
<span style="color: black;"><<span style="color: black;">td</span>></span>{{ row.学号 }}<span style="color: black;"></<span style="color: black;">td</span>></span>
<span style="color: black;"><<span style="color: black;">td</span>></span>{{ row.姓名 }}<span style="color: black;"></<span style="color: black;">td</span>></span>
<span style="color: black;"><<span style="color: black;">td</span>></span>{{ row.语文 }}<span style="color: black;"></<span style="color: black;">td</span>></span>
<span style="color: black;"><<span style="color: black;">td</span>></span>{{ row.数学 }}<span style="color: black;"></<span style="color: black;">td</span>></span>
<span style="color: black;"><<span style="color: black;">td</span>></span>{{ row.英语 }}<span style="color: black;"></<span style="color: black;">td</span>></span>
<span style="color: black;"></<span style="color: black;">tr</span>></span>
{% endfor %}
<span style="color: black;"></<span style="color: black;">tbody</span>></span>
<span style="color: black;"></<span style="color: black;">table</span>></span>
<span style="color: black;"></<span style="color: black;">div</span>></span>
<span style="color: black;"></<span style="color: black;">div</span>></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&from=article.pc_detail&lk3s=953192f4&x-expires=1728744684&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;"><div class="container"></div></span>内的表格上方<span style="color: black;">增多</span>一个<span style="color: black;"><div class="row"></div></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;"><<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"container"</span>></span><span style="color: black;"><!--做一个简单的布局--></span>
<span style="color: black;"><<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"row"</span>></span>
<span style="color: black;"><<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>></span>
{% csrf_token %} <span style="color: black;"><!--设置一个token令牌,这是Django的<span style="color: black;">守护</span>机制,<span style="color: black;">无</span>这个参数,会报错--></span>
<span style="color: black;"><<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"form-group mb-2"</span>></span>
<span style="color: black;"><<span style="color: black;">label</span> <span style="color: black;">for</span>=<span style="color: black;">"staticEmail2"</span> ></span>成绩<span style="color: black;">查找</span><span style="color: black;"></<span style="color: black;">label</span>></span>
<span style="color: black;"></<span style="color: black;">div</span>></span>
<span style="color: black;"><<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"form-group mx-sm-3 mb-2"</span>></span>
<span style="color: black;"><<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>></span>
<span style="color: black;"></<span style="color: black;">div</span>></span>
<span style="color: black;"><<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>></span><span style="color: black;">查找</span><span style="color: black;"></<span style="color: black;">button</span>></span>
<span style="color: black;"></<span style="color: black;">form</span>></span>
<span style="color: black;"></<span style="color: black;">div</span>></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&from=article.pc_detail&lk3s=953192f4&x-expires=1728744684&x-signature=JcjQ8o2gKfpBYQBguNzNFakFQfY%3D" style="width: 50%; margin-bottom: 20px;"></div>
页:
[1]