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())
  • 相关阅读:
    Python函数高级
    Python 2和3的区别
    GIL,python全局解释器锁
    Python中的 list
    python中的单例
    新式类和经典类
    整理的排序算法
    Python的双下划方法
    Python 中闭包函数和装饰器
    面向对象,特性之继承
  • 原文地址:https://www.cnblogs.com/tangpg/p/9004476.html
Copyright © 2011-2022 走看看