Django视图有哪些类型?

2025-12-02 0 2,909

本文教程操作环境:windows7系统、django2.1,DELL G3电脑。

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视图的类型,大家对基础的内容有所掌握后,可以动手尝试下代码部分的运行,加深对两种不同视图的理解。更多Python框架指路:django

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 python框架 Django视图有哪些类型? https://www.zhiup.top/6348.html

相关