这篇文章将为大家详细讲解有关Django中有哪些视图类型,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1、基于功能的视图
基于函数的视图是使用python中的函数编写的,该函数以HttpRequest对象作为参数并返回HttpResponse对象。基于功能的视图通常分为4种基本策略,即CRUD(创建,检索,更新,删除)。CRUD是用于开发的任何框架的基础。
# import the standard Django Model
# from built-in library
from django.db import models
# declare a new model with a name "GeeksModel"
class GeeksModel(models.Model):
# fields of the model
title = models.CharField(max_length = 200)
description = models.TextField()
# renames the instances of the model
# with their title name
def __str__(self):
return self.title
2、基于类的视图
基于类的视图提供了一种将视图实现为Python对象而非函数的替代方法。与基于函数的视图相比,基于类的视图更易于管理。
from django.views.generic.list import ListView
from .models import GeeksModel
class GeeksList(ListView):
# specify the model for list view
model = GeeksModel
关于Django中有哪些视图类型就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。