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())
  • 相关阅读:
    C++ string char[] 转化
    c++ 转化
    2014/4/16
    2014/4/11
    垂直电商现倒闭潮
    经典K线组合图解 > 正文
    上下影线
    分​析​主​力​试​盘​手​法
    nginx重新编译不停服
    nexus
  • 原文地址:https://www.cnblogs.com/tangpg/p/9004476.html
Copyright © 2011-2022 走看看