【Django】django模板与前端技术(html模板)

文章目录

"python包html"还是"html包python"?

在前端页面中html代码比python多得多,所以一定是html包python最优!于是引出今天的模板。

大体分为三个步骤:

  1. 新建templates文件夹,写html模板;
  2. 模板语法> 变量语法:{{变量}};代码块语法:{%for%}、{%end for%}
  3. 在views.py中,使用render.hmtl返回。

1.新建模板

  • app目录下新建文件夹"templates"
  • 新建show_excle.html
  • 选择语言



    快速生成html骨架模板

    格式化代码方法:

2.模板语法

python 复制代码
 <table>
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>语文</th>
            <th>数学</th>
            <th>英语</th>
        </tr>
        {% for idx, row in df.iterrows %}
        <tr>
            <td>{{row.学号}}</td>
            <td>{{row.姓名}}</td>
            <td>{{row.语文}}</td>
            <td>{{row.数学}}</td>
            <td>{{row.英语}}</td>
        </tr>
        {% endfor %}
       
    </table>

3.views.py

python 复制代码
from django.shortcuts import render
from django.http import HttpResponse
from antproject.settings import BASE_DIR
import pandas as pd

# Create your views here.
def hello(request):
    return HttpResponse("this is hello/")

def show_excel(request):
    df=pd.read_excel(BASE_DIR / "data/score.xlsx")
    return render(request,"show_excel.html",{"df":df})

测试

相关推荐
b***748819 分钟前
前端GraphQL案例
前端·后端·graphql
云飞云共享云桌面20 分钟前
无需配置传统电脑——智能装备工厂10个SolidWorks共享一台工作站
运维·服务器·前端·网络·算法·电脑
ganshenml42 分钟前
sed 流编辑器在前端部署中的作用
前端·编辑器
0***K8921 小时前
Vue数据挖掘开发
前端·javascript·vue.js
蓝胖子的多啦A梦1 小时前
ElementUI表格错位修复技巧
前端·css·vue.js·el-table表格错位
_OP_CHEN2 小时前
前端开发实战深度解析:(一)认识前端和 HTML 与开发环境的搭建
前端·vscode·html·web开发·前端开发
喂自己代言2 小时前
HTML ``元素:链接外部资源的关键角色与用法
css·html
xiAo_Ju2 小时前
iOS一个Fancy UI的Tricky实现
前端·ios
H***99762 小时前
Vue深度学习实战
前端·javascript·vue.js
toooooop82 小时前
Vuex 中 state、mutations 和 actions 的原理和写法
前端·javascript·uni-app