首页 > 其他 > 详细

模板加载

时间:2021-01-11 23:01:27      阅读:34      评论:0      收藏:0      [点我收藏+]
  • 加载模板
  • django.template.loader 这个模块提供了两种方法加载模板
  • 加载指定模板并返回Template对象
get_template(template_name, using=None)
  • 加载指定模板并返回Template对象
select_template(template_name_list, using=None)
  • 从 文件中加载
from django.template import Context, loader
def index(request):
  t = loader.get_template("test.html")
  context = {"name": "study python !!!"}         #字典可以在html文件用{{ name }}获取值,name相当于一个变量,固定写法
  return HttpResponse(t.render(context, request))
  • 快捷方式和上面方法效果一样
from django.shortcuts import render
  def index(request):
    context = {name: "study python"}
    return render(request, test.html, context)  #request必传的参数,当前请求都存在request参数里面

 

模板加载

原文:https://www.cnblogs.com/jiaqili/p/14264462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!