品易云推流 关闭
文章详情页
文章 > Django > Django视图有哪些类型?

Django视图有哪些类型?

头像

小妮浅浅

2021-03-24 09:53:264470浏览 · 0收藏 · 0评论

本文教程操作环境: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

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图