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

测试

相关推荐
kyriewen23 分钟前
面试官问我:AI 都能写代码了,前端凭什么还值 25K
前端·javascript·人工智能
风骏时光牛马1 小时前
AI源码分析:拆解模型底层实现逻辑
前端
IT_陈寒1 小时前
React子组件莫名其妙重渲染?你可能漏了这个Hook
前端·人工智能·后端
乘风gg2 小时前
DeepSeek V4.1 Flash 来了,明天中午 Flash 降价 60%
前端·ai编程·claude
默_笙2 小时前
❗ 给金鱼装记忆(上):从白板到笔记本,AI 的短期记忆生存指南
前端·javascript
酷酷的逗逗乐2 小时前
前端转 Agent 开发 · 第五节:Memory 会话记忆
前端·程序员
掘金挖土2 小时前
前端手摸手跑路之 AI 应用开发(二)
前端·后端
mayaairi3 小时前
Vue2 组件通讯(四):v-model、scoped样式、mixins与plugins
前端·javascript·vue.js
梦曦i3 小时前
@meng-xi/uni-router 未来展望:夯实基础、深化体验、探索前沿
前端·uni-app
Yeyu3 小时前
AAOS AppCard 实践:怎么把自己的 App 塞进别人的卡片里
前端