zoukankan      html  css  js  c++  java
  • 111:TemplateView讲解

    TemplateView:

      django.views.generic.base.TemplateView,这个类视图是专门用来返回模版的。在这个类中,有两个属性是经常需要用到的,一个是template_name,这个属性是用来存储模版的路径,TemplateView会自动的渲染这个变量指向的模版。另外一个是get_context_data,这个方法是用来返回上下文数据的,也就是在给模版传的参数的。示例代码如下:

    from django.views.generic.base import TemplateView
    
    urlpatterns = [
        path('about/', TemplateView.as_view(template_name='about.html')),
      
    # 如果就是一个纯静态页面,就可以这样搞 ]

    如果有参数可以如下搞:

    from django.views.generic.base import TemplateView
    
    class aboutPageView(TemplateView):
    
        template_name = "about.html"
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            context['username'] = "你大爷的"
            return context

    urls.py中的映射代码如下:

    from django.urls import path
    from myapp.views import aboutPageView
    
    urlpatterns = [
        path('', aboutPageView.as_view(), name='about'), 
    ]
  • 相关阅读:
    HTML5中的canvas
    预解释
    asp.net中的CheckBox控件的使用
    jQuery 效果
    常用的 jQuery 事件
    轮播图
    迭代器与生成器
    js对象拷贝
    事件循环、同步异步、宏任务微任务
    Vue 的 keep-alive 组件缓存
  • 原文地址:https://www.cnblogs.com/zheng-weimin/p/10428346.html
Copyright © 2011-2022 走看看