In Django template encountered "context must be a dict rather

原代码:
Using template files # 
# 1. Load the template file, the template file acquiring a 
TEMP = loader.get_template ( 'booktest / index.html') 

# 2. context template is defined: a template data file delivery 
context = RequestContext (request, {} ) 

render template # 3: generate standard html content 
res_html = temp.render (context) 

# 4. returned to the browser 
return HttpResponse (res_html)

  

Error message:

Exception Type: TypeError at /index
Exception Value: context must be a dict rather than RequestContext.

Proper code: 

# 1. The template files are loaded 
TEMP = loader.get_template ( 'booktest / index.html') 

# 2. context template is defined: a template data file delivery 
context the RequestContext = (Request, {}) 
context.push (about locals ( )) 

# 3 template rendering: generate standard html content 
res_html = temp.render (context = about locals (), Request = Request) 

# 4. returned to the browser 
return HttpResponse (res_html)

  

原代码:
# 使用模板文件
# 1.加载模板文件
temp = loader.get_template('booktest/index.html')

# 2.定义模板上下文:给模板文件传递数据
context = RequestContext(request, {})

# 3.模板渲染:产生标准的html内容
res_html = temp.render(context)

# 4.返回给浏览器
return HttpResponse(res_html)

Error message:

Exception Type: TypeError at /index
Exception Value: context must be a dict rather than RequestContext.

Correct codes:

1. Load template files # 
TEMP = loader.get_template ( 'booktest / index.html') 

# 2. context defined template: the template file is transmitted to the data 
context the RequestContext = (Request, {}) 
context.push (about locals ()) 

# 3. template rendering: generate standard html content 
res_html = temp.render (context = about locals (), Request = Request) 

# 4. returned to the browser 
return HttpResponse (res_html)

Guess you like

Origin www.cnblogs.com/mibao/p/11899462.html