• 技术文章 >Python框架 >Django

    Django视图有哪些类型?

    小妮浅浅小妮浅浅2021-03-24 09:53:26原创5208

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

    1、基于功能的视图

    基于函数的视图是使用python中的函数编写的,该函数以HttpRequest对象作为参数并返回HttpResponse对象。基于功能的视图通常分为4种基本策略,即CRUD(创建,检索,更新,删除)。CRUD是用于开发的任何框架的基础。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    # 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对象而非函数的替代方法。与基于函数的视图相比,基于类的视图更易于管理。

    1

    2

    3

    4

    5

    6

    7

    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

    专题推荐:django视图类型
    上一篇:Django创建应用程序并呈现的方法 下一篇:django之MVT模式介绍

    相关文章推荐

    • Django中form是什么?• Django中如何创建模型?• Django中CRUD操作有哪些?• Django中如何创建视图?• Django创建应用程序并呈现的方法

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网