【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})

测试

相关推荐
摆烂大大王1 小时前
玩转 OpenClaw:用 TaskFlow + Heartbeat 打造自动化工作流
前端·人工智能·自动化
zhangxingchao1 小时前
AI 大模型核心六:量化、Workflow 与 Agent、多轮 RAG
前端·人工智能·后端
梦想的颜色2 小时前
TypeScript 完全指南(上):从零开始掌握类型系统
前端·typescript
之歆2 小时前
Day01_ES6+ 专业指南:从基础到实战的现代JavaScript开发(下)
前端·javascript·es6
lichenyang4532 小时前
鸿蒙 MVVM 实战:从 Demo 到工程化,聊聊登录、状态管理与埋点系统设计
前端
IT_陈寒2 小时前
Vite打包时遇到的坑,原来问题出在这里
前端·人工智能·后端
kyriewen3 小时前
AI生成代码快如闪电,但我修了三个小时——它到底帮了谁?
前端·javascript·ai编程
ayqy贾杰4 小时前
基层管理的三板斧,在AI时代行不通了
前端·后端·团队管理
Apifox4 小时前
Apifox 5 月更新|Postman 导入优化、Runner 支持非 root 运行、请求代码自动带鉴权
前端·后端·安全
miaowmiaow4 小时前
PSD2Code 近期更新与深度解析:从设计稿到生产级代码的完整技术栈
前端·人工智能·ai编程