Django模版html-基于动态数据的展示

本文介绍了在Django中如何在views.py文件中向HTML传递数据,包括使用HttpResponse和render两种方式,并详细讲解了HTML模板如何接收并处理这些数据,如变量、列表、字典的使用,以及过滤器、条件判断标签、循环标签和安全相关的csrf_token等的运用。
摘要由CSDN通过智能技术生成

一, 在views.py文件中新增一个对象,用于向html提交数据

方式一: HttpResponse,无法提交后端数据库数据

def index(request):
    return HttpResponse("welcome to django!")

方式二:render,可提交后端数据库数据

def html_index(request):
    title="个人博客"
    categories=models.Category.objects.all()
    data={"title":title,"categories":categories,"articles":articles}
    return render(request,'index.html',data)


def html_index(request):
    title="个人博客"
    categories=models.Category.objects.all()
    return render(request,'index.html',locals())


render()函数,通过context传递数据给模版:
help文档中render描述:
    render(request, template_name, context=None, content_type=None, status=None, using=None)
参数:
    request: 是一个固定参数
    template_name: templates中定义的文件,注意路径名。比如:"templates/polls/index.html", 则参数
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值