浅谈django的render函数的参数问题
作者:慢慢的踏实走 时间:2022-07-10 18:39:20
hello.html 文件代码如下:
HelloWorld/templates/hello.html 文件代码:
<h1>{{ hello }}</h1>
HelloWorld/HelloWorld/view.py 文件代码:
# -*- coding: utf-8 -*-
#from django.http import HttpResponse
from django.shortcuts import render
def hello(request):
context = {}
context['hello'] = 'Hello World!'
return render(request, 'hello.html', context)
ontext 字典中元素的键值 "hello" 对应了模板中的变量 "{{ hello }}"。
一旦你创建一个 Template 对象,你可以用 context 来传递数据给它。 一个context 是一系列变量和它们值的集合。
context 在 Django 里表现为 Context 类,在 django.template 模块里。它的构造函数带有一个可选的参数: 一个字典映射变量和它们的值。 调用 Template 对象 的 render() 方法并传递 context 来填充模板:
>>> from django.template import Context, Template
>>> t = Template('My name is {{ name }}.')
>>> c = Context({'name': 'nowamagic'})
>>> t.render(c)
u'My name is nowamagic.'
我们必须指出的一点是,t.render(c) 返回的值是一个 Unicode 对象,不是普通的 Python 字符串。 你可以通过字符串前的 u 来区分。 在框架中,Django 会一直使用 Unicode 对象而不是普通的字符串。 如果你明白这样做给你带来了多大便利的话,尽可能地感激 Django 在幕后有条不紊地为你所做这这么多工作吧。 如果不明白你从中获益了什么,别担心。你只需要知道 Django 对 Unicode 的支持,将让你的应用程序轻松地处理各式各样的字符集,而不仅仅是基本的A-Z英文字符。
from django.shortcuts import render
help文档中描述如下:
render(request, template_name, context=None, content_type=None, status=None, using=None)
Returns a HttpResponse whose content is filled with the result of calling django.template.loader.render_to_string() with the passed arguments.
此方法的作用---结合一个给定的模板和一个给定的上下文字典,并返回一个渲染后的 HttpResponse 对象。
通俗的讲就是把context的内容, 加载进templates中定义的文件, 并通过浏览器渲染呈现.
参数讲解:
request: 是一个固定参数, 没什么好讲的。
template_name: templates 中定义的文件, 要注意路径名. 比如'templates\polls\index.html', 参数就要写‘polls\index.html'
context: 要传入文件中用于渲染呈现的数据, 默认是字典格式
content_type: 生成的文档要使用的MIME 类型。默认为DEFAULT_CONTENT_TYPE 设置的值。
status: http的响应代码,默认是200.
using: 用于加载模板使用的模板引擎的名称。
来源:https://blog.csdn.net/u013176681/article/details/73844330
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python Pygame实现俄罗斯方块
![](https://img.aspxhome.com/file/2023/8/77898_0s.jpg)
Python实现希尔排序算法的原理与用法实例分析
![](https://img.aspxhome.com/file/2023/8/112918_0s.jpg)
Python Flask-Login实现用户会话管理
![](https://img.aspxhome.com/file/2023/9/90589_0s.png)
Laravel实现队列的示例代码
在IE中使用高级CSS3选择器
慎用UL列表
![](https://img.aspxhome.com/file/UploadPic/20093/25/01-71s.gif)
css有趣而诡异的数组
解决CentOS下ImportError: No module named '_sqlite3'的问题
历数Firefox2.0对XML处理的改进
![](https://img.aspxhome.com/file/UploadPic/200711/27/20071127125113381s.jpg)
安装navicat最新详细流程
![](https://img.aspxhome.com/file/2023/6/124496_0s.png)
妙用dw图层与表格进行网页布局
![](https://img.aspxhome.com/file/UploadPic/20072/200723113145358s.jpg)
javascript手风琴下拉菜单实现代码
![](https://img.aspxhome.com/file/2023/1/56591_0s.png)
超级给力的JavaScript的React框架入门教程
vue-cli npm如何解决vue项目中缺失core-js的问题
Python从文件中读取数据的方法步骤
![](https://img.aspxhome.com/file/2023/7/90657_0s.png)
Mybatis出现ORA-00911: invalid character的解决办法
![](https://img.aspxhome.com/file/2023/9/65689_0s.png)
Python实现微信机器人的方法
![](https://img.aspxhome.com/file/2023/4/64714_0s.jpg)
python实现将list拼接为一个字符串
Python实现图片滑动式验证识别方法
![](https://img.aspxhome.com/file/2023/3/94183_0s.png)
Go到底能不能实现安全的双检锁(推荐)
![](https://img.aspxhome.com/file/2023/4/133934_0s.jpg)