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

测试

相关推荐
Linsk5 小时前
前端代码压缩对浏览器兼容性的影响
前端
yingyima5 小时前
凌晨3点的闹钟:分布式定时任务设计实战
前端
用户81423861188415 小时前
iOS ObjectC棕地应用集成Expo(React Native)的过程及踩坑
前端
lichenyang4536 小时前
HMRouter 完整能力清单:从初始化到拦截器/对话框/生命周期/转场动画一站式查阅
前端
我有2只猫6 小时前
LabelStudio二次开发
人工智能·python·django·ocr
lichenyang4536 小时前
鸿蒙电商 Demo v2:真实商品接口 + 支付/订单闭环 + 收藏功能,外加一个 ArkUI V2 @Builder 响应式断链的硬核坑
前端·后端
前端的阶梯6 小时前
如何节省你的token,请看CodeGraph
前端·人工智能·后端
万少6 小时前
产品原型不用从零画 -GPT 出图,Gemini 生成 HTML
前端·javascript·后端
wuhen_n7 小时前
RAG 第一步:多格式文档加载与文本预处理实战
前端·langchain·ai编程
程序员黑豆7 小时前
全新系列开启:AI 全栈开发
前端·后端·全栈