Table of Contents
Class view
View
TemplateView" >TemplateView
is a detailed view of one of the many
ListView

class view

Jun 23, 2017 pm 04:30 PM
view

Class view

Usage principles
  • Code Less is better

  • Never duplicate code

  • View should only contain presentation logic, not business logic

  • Keep view logic clear and simple

  • Don’t use CBVs as error handlers for 403, 404, 500

  • Keep Mixin is simple and clear

mixin
In programming, mixin refers to A class that inherits it provides additional functionality, but it cannot be used alone
In programming languages ​​with multiple inheritance capabilities, mixins can add additional functionality or methods to the class. In Django, we can Use mixins to provide more extensibility for CBVs. Of course, during the class inheritance process, we recommend the following principles:
  • The View provided by Django itself is always on the far right

  • mixin is on the left side of the above view in turn

  • mixin always inherits from Python’s object type

Django mixin Library:
Commonly used class views
View                    基本View 可以在任何时候使用 见后面详细介绍
RedirectView            重新定向到其他URL 将访问"/log-in/"的用户重新定向到"/login/"TemplateView            显示Django 模板 一般网站中使用模板显示的页
ListView                显示对象列表 文章列表页
DetailView              显示对象详情 文章详细页
FormView                提交From 网站联系我们或emai订阅form
CreateView              创建对象 创建新文章页
UpdateView              更新对象 修改文章页
DeleteView              删除对象 删除文章页
Generic date views      显示一段时间内的对象 按时间归类的博客
Copy after login

View

django.views.generic.base.View
The core of the class-based base view. All other class-based views inherit from this base class.
 django.shortcuts  django.views.generic state =http_method_names = [, , , , , , ,  dispatch(self, request, *args, **(= super(Index,self).dispatch(request, *args, **( HttpResponse( HttpResponse()
Copy after login

TemplateView

Inherits methods and properties from the following views
  1. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="kwd">generic<span class="pun">.<span class="pln">base<span class="pun">.<span class="typ">TemplateResponseMixin</span></span></span></span> </span></span></span></span></span>

  2. #django<span class="pln">.<span class="pun">views<span class="pln">.<span class="pun">generic<span class="kwd">.<span class="pun">base<span class="pln">.<span class="pun">ContextMixin<span class="typ"></span></span></span></span></span></span></span></span></span>

  3. ##django.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="kwd">base<span class="pun">.<span class="pln">View <span class="pun"><span class="typ"></span></span></span></span></span></span></span></span></span>

#Example
 django.views.generic.base =  dispatch(self, request, *args, **(= super(HomePageView,self).dispatch(request, *args, **( get_context_data(self, **= super(HomePageView, self).get_context_data(**] = [,,( context
Copy after login

RedirectView

Inherits methods and properties from:

    ##django
  1. .

    views.<span class="pln">generic<span class="pun">.<span class="pln">base<span class="pun">.<span class="kwd">View<span class="pun"><span class="pln"><span class="pun"><span class="typ"></span></span> </span></span></span></span></span></span></span>

##Example
 django.views.generic.base == False  query_string = True  pattern_name =    dispatch(self, request, *args, **(= super(ArticleRedirectView,self).dispatch(request, *args, **( get_redirect_url(self, *args, **( super(ArticleRedirectView, self).get_redirect_url(*args, **kwargs)
Copy after login

DetaliView
DetailView

is a detailed view of one of the many

object

of these publishers Parsing, the main data it obtains is an object<span class="typ">. <span class="pun"><span class="pln"><span class="pun"><span class="pln"><span class="pun"></span></span></span></span></span></span>Inherits methods and properties from the following views:

  1. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">detail<span class="pun">.<span class="typ">SingleObjectTemplateResponseMixin</span></span></span></span></span></span></span></span></span>

  2. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">base<span class="pun">.<span class="typ">TemplateResponseMixin</span></span></span></span></span></span></span></span></span>

  3. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">detail<span class="pun">.<span class="typ">BaseDetailView</span></span></span></span></span></span></span></span></span>

  4. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">detail<span class="pun">.<span class="typ">SingleObjectMixin</span></span></span></span></span></span></span></span></span>

  5. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">base<span class="pun">.<span class="typ">View</span></span></span></span></span></span></span></span></span>

 
示例
 django.contrib.auth.mixins  chouti.models ==   pk_url_kwarg =   

 get_context_data(self, **(context = super(UserDetailView, self).get_context_data(**] = [33, 44]
Copy after login

 

 

ListView

对于ListView来说,它需要显示的是多个object的信息,返回的是一个对象列表
它的数据来源是model和queryset这两个属性:
  1. <span class="pln">model<span class="pun">:获取这个<span class="pln">model<span class="pun">相关的所有数据</span></span></span></span>

  2. <span class="pln">queryset<span class="pun">:从这个<span class="pln">queryset<span class="pun">中获取指定的数据。</span></span></span></span>

  3. <span class="pln">queryset<span class="pun">的优先级高于<span class="pln">model</span></span></span>

  4. <span class="pun">它所获取的数据是通过<span class="pln">context<span class="pun">中<span class="pln">object_list<span class="pun">这个变量来传递的</span></span></span></span></span>

 
 
从以下视图继承方法和属性:
  1. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">list<span class="pun">.<span class="typ">MultipleObjectTemplateResponseMixin</span></span></span></span></span></span></span></span></span>

  2. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">base<span class="pun">.<span class="typ">TemplateResponseMixin</span></span></span></span></span></span></span></span></span>

  3. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">list<span class="pun">.<span class="typ">BaseListView</span></span></span></span></span></span></span></span></span>

  4. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">list<span class="pun">.<span class="typ">MultipleObjectMixin</span></span></span></span></span></span></span></span></span>

  5. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">gen</span></span></span></span></span>

 
 
示例
 blog.models  django.views.generic model = Post   queryset = Post.objects.filter(title__contains = )  context_object_name =   template_name=   ( Post.objects.filter(title__contains = self.kwargs[ get_context_data(self, **(= super(PublisherList, self).get_context_data(**kwargs)  context[] = [, ]    context
Copy after login

 

 
 

The above is the detailed content of class view. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

iOS 17's standby mode turns a charging iPhone into a home hub iOS 17's standby mode turns a charging iPhone into a home hub Jun 06, 2023 am 08:20 AM

In iOS 17 Apple is introducing Standby Mode, a new display experience designed for charging iPhones in a horizontal orientation. In this position, the iPhone is able to display a series of full-screen widgets, turning it into a useful home hub. Standby mode automatically activates on an iPhone running iOS 17 placed horizontally on the charger. You can view time, weather, calendar, music controls, photos, and more. You can swipe left or right through the available standby options and then long press or swipe up/down to customize. For example, you can choose from analog view, digital view, bubble font, and daylight view, where the background color changes based on time as time passes. There are some options

How to implement editable tables in Vue How to implement editable tables in Vue Nov 08, 2023 pm 12:51 PM

Tables are an essential component in many web applications. Tables usually have large amounts of data, so tables require some specific features to improve user experience. One of the important features is editability. In this article, we will explore how to implement editable tables using Vue.js and provide specific code examples. Step 1: Prepare the data First, we need to prepare the data for the table. We can use a JSON object to store the table's data and store it in the data property of the Vue instance. In this case

Understand the differences and comparisons between SpringBoot and SpringMVC Understand the differences and comparisons between SpringBoot and SpringMVC Dec 29, 2023 am 09:20 AM

Compare SpringBoot and SpringMVC and understand their differences. With the continuous development of Java development, the Spring framework has become the first choice for many developers and enterprises. In the Spring ecosystem, SpringBoot and SpringMVC are two very important components. Although they are both based on the Spring framework, there are some differences in functions and usage. This article will focus on comparing SpringBoot and Spring

How to use CodeIgniter4 framework in php? How to use CodeIgniter4 framework in php? May 31, 2023 pm 02:51 PM

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

Laravel development: How to generate views using Laravel View? Laravel development: How to generate views using Laravel View? Jun 14, 2023 pm 03:28 PM

Laravel is one of the most popular PHP frameworks currently, and its powerful view generation capabilities are impressive. A view is a page or visual element displayed to the user in a web application, which contains code such as HTML, CSS, and JavaScript. LaravelView allows developers to use a structured template language to build web pages and generate corresponding views through controllers and routing. In this article, we will explore how to generate views using LaravelView. 1. What

What are the views in Word? What are the views in Word? Mar 19, 2024 pm 06:10 PM

I guess that many students want to learn the typesetting skills of Word, but the editor secretly tells you that before learning the typesetting skills, you need to understand the word views clearly. In Word2007, 5 views are provided for users to choose. These 5 views include pages. View, reading layout view, web layout view, outline view and normal view, let’s learn about these 5 word views with the editor today. 1. Page view Page view can display the appearance of the print result of the Word2007 document, which mainly includes headers, footers, graphic objects, column settings, page margins and other elements. It is the page view closest to the print result. 2. Reading layout view Reading layout view displays Word2007 documents and Office in the column style of a book

Detailed explanation of views in Django framework Detailed explanation of views in Django framework Jun 17, 2023 am 10:18 AM

Django is a highly customizable web framework that provides many convenient tools and libraries to help developers quickly create high-performance, scalable web applications. Among them, views are one of the most important components of the Django framework. Views are responsible for processing requests from clients and returning corresponding responses. In this article, we'll take a deep dive into views in the Django framework and explain how to use it to create high-performance, customizable web applications. 1. The basic concept of views in Django

Microsoft updates Visual Studio Code 1.80 Microsoft updates Visual Studio Code 1.80 Jul 10, 2023 pm 08:13 PM

Microsoft recently launched Visual Studio Code 1.80. Although this update was released in July, the official still calls it the June update. After users install this update, the built-in terminal can directly display images, and auxiliary functions have also been optimized. In the previously released preview version of Visual Studio Code, support for images in the terminal has been supported, and it is enabled by default in the new 1.80 version. In order to be able to display the image in the terminal, the image pixel data is converted into text through a special escape sequence and finally written to the terminal. If you want to pipe typical PNG, GIF or JPEG files to the terminal, you need to install the imgcatpython package and then run imgc in the terminal

See all articles