Use Django's view class TemplateView to easily and quickly integrate templates, views and template variables

TemplateViewis one of the generic view classes provided by Django, which allows you to associate templates with views without writing any Python code. Here is TemplateViewsome introduction about the class:

  1. Render Template: TemplateViewResponsible for rendering the specified template and returning the generated HTML response. You simply provide the template name or path and TemplateViewit will be automatically associated with the appropriate template engine.

  2. URL configuration: You can TemplateViewassociate a specific URL with URL configuration. This means that when a user requests a page that matches that URL, Django will automatically call TemplateViewto handle the request.

  3. Context data: You can get_context_dataprovide additional context data to templates by overriding methods. This allows you to pass some custom variables or objects to the template before rendering it.

  4. Advantages of class view: Class view has many advantages over function view. They make code more modular, easier to maintain and extend. TemplateViewAs a form of class view, it provides developers with a convenient way to deal with simple pages that don't require much logic processing.

By using it TemplateView, you can easily associate templates, views, and template variables without writing a lot of repetitive code, which is very convenient for building simple static pages.

Here is a simple example showing how to use TemplateViewthe class:
Code in E:\Python_project\P_001\myshop-test\myshop\app1\views_class.py:

# -*- coding: utf-8 -*-

from django.views.generic import TemplateView


class TestTemplateView(TemplateView):
    # 设置模板文件
    template_name = "index.html"

    #  重写父类TemplateView中的get_context_data()方法
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # 增加模板变量info
        context["info"] = "这里是昊虹君写的技术笔记-昊虹AI笔记"
        return context

Code in E:\Python_project\P_001\myshop-test\myshop\templates\index.html:

<div>
    这里是一个关于视图类TemplateView的示例
    <br>
    以下是通过重写父类TemplateView中的get_context_data()方法引入的新写的变量'info'的值
    <br>
   {
   
   {info}}
</div>

Code in E:\Python_project\P_001\myshop-test\myshop\myshop\urls.py:

from django.urls import path
from app1.views_class import TestTemplateView

urlpatterns = [
    path('test_templateview/', TestTemplateView.as_view()),
]

Run the Django application:

CD E:\Python_project\P_001\myshop-test\myshop
E:
python manage.py runserver 127.0.0.1:8010

Visit the URL path below:

http://127.0.0.1:8010/test_templateview/

The effect is as follows:
insert image description here
It can be seen that templates and variables in the view layer are easily integrated through the view class TemplateView.

This blog code github link:
https://github.com/haohongcode/Django_learning/blob/main/2023-06/myshop-2023-06-23-01-TemplateView.zip

Guess you like

Origin blog.csdn.net/wenhao_ir/article/details/131342937