zoukankan      html  css  js  c++  java
  • django-渲染页面+locals

    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())
  • 相关阅读:
    ural1238. Folding(记忆化)
    URAL1410. Crack
    树套树Day1线段树套平衡树bzoj3196
    noipd2t3列队
    NOIP2017D1T3
    uoj279温暖会指引我们前行
    一篇打脸文
    Link-Cut Tree
    重口味费用流
    bzoj1000~1025
  • 原文地址:https://www.cnblogs.com/tangpg/p/9004476.html
Copyright © 2011-2022 走看看