from django.shortcuts import render, redirect from django.views import View from django.http import HttpResponse from django.template.loader import get_template class CommonRenderHtmlRequest(View): ''' 测试渲染页面1 render ''' def get(self, request): return render(request, 'ces.html') # 直接渲染页面 class GetTemplateFunc(View): ''' 测试渲染页面2 get_template 更加高级''' def get(self, request): t = get_template('ces.html') # Template class对象 <django.template.backends.django.Template object at 0xb51c7c6c> print t.render() # 这儿是字符串形式 return HttpResponse(t.render()) # 所以用HttpResponse
动态渲染
class DynamicRendering(View): ''' 动态渲染 ''' def get(self, request): message = '贼帅' name = 'tj' age = 18 # locals() 获取当前能访问的所有变量,生成一个字典{'request': <WSGIRequest: GET '/hello/ces3/'>, # 'age': 18, 'message': u'u8d3cu5e05', 'name': u'tj', # 'self': <hello.views.DynamicRendering object at 0xb58e322c>} print locals() return render(request, 'ces1.html', context=locals())