在某些情况下编写视图代码,正如我们所看到前面工作真的很繁重。想象一下,只需要一个静态页面或列表页。 Django还提供了一种简单的方法来设置这些简单的视图被称为泛型视图。
不同于传统的视图,一般的视图是一个类但没有函数。Django还提供了一组类 django.views.generic 泛型视图,以及每一个普通视图是这些类或从它们中的一个类继承的。
有10+泛型类−
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.nhooo.com # Date : 2020-08-08 >>> import django.views.generic >>> dir(django.views.generic) ['ArchiveIndexView', 'CreateView', 'DateDetailView', 'DayArchiveView', 'DeleteView', 'DetailView', 'FormView', 'GenericViewError', 'ListView', 'MonthArchiveView', 'RedirectView', 'TemplateView', 'TodayArchiveView', 'UpdateView', 'View', 'WeekArchiveView', 'YearArchiveView', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'base', 'dates', 'detail', 'edit', 'list']
你可以使用泛型视图。让我们来看一些实例来看看它是如何工作的。
让我们从“static.html”模板发布静态页面。
我们的 static.html −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.nhooo.com # Date : 2020-08-08 <html> <body> This is a static page!!! </body> </html>
如果我们这样做,按以前学过的方式,我们将不得不改变 myapp/views.py −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.nhooo.com # Date : 2020-08-08 from django.shortcuts import render def static(request): return render(request, 'static.html', {})
myapp/urls.py 如下 −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.nhooo.com # Date : 2020-08-08 from django.conf.urls import patterns, url urlpatterns = patterns("myapp.views", url(r'^static/', 'static', name = 'static'),)
最好的办法就是使用泛型视图。对于这一点,我们的 myapp/views.py 将变成为 −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.nhooo.com # Date : 2020-08-08 from django.views.generic import TemplateView class StaticView(TemplateView): template_name = "static.html"
而我们的 myapp/urls.py 将如下 −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.nhooo.com # Date : 2020-08-08 from myapp.views import StaticView from django.conf.urls import patterns urlpatterns = patterns("myapp.views", (r'^static/, StaticView.as_view()),) , StaticView.as_view()),)
当访问 /myapp/static 将得到 −
出于同样的结果,我们也可以,执行下列操作 −
不需要修改 views.py 更改url.py文件为 -
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.nhooo.com # Date : 2020-08-08 from django.views.generic import TemplateView from django.conf.urls import patterns, url urlpatterns = patterns("myapp.views", url(r'^static/',TemplateView.as_view(template_name = 'static.html')),)
正如你所看到的,只需要改变url.py文件中的第二个方法。
我们要列出所有条目在Dreamreal模型。这样使用ListView泛型视图类变得容易。编辑url.py文件,并对其进行更新 -
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.nhooo.com # Date : 2020-08-08 from django.views.generic import ListView from django.conf.urls import patterns, url urlpatterns = patterns( "myapp.views", url(r'^dreamreals/', ListView.as_view(model = Dreamreal, template_name = "dreamreal_list.html")), )
重要的是要注意,在这一点上是变量通由泛型视图到模板为object_list。如果你想自己的名字,将需要一个 context_object_name 参数添加到as_view方法。然后 url.py 成为 -
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.nhooo.com # Date : 2020-08-08 from django.views.generic import ListView from django.conf.urls import patterns, url urlpatterns = patterns("myapp.views", url(r'^dreamreals/', ListView.as_view( template_name = "dreamreal_list.html")), model = Dreamreal, context_object_name = ”dreamreals_objects” ,)
然后关联的模板将成为 −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.nhooo.com # Date : 2020-08-08 {% extends "main_template.html" %} {% block content %} Dreamreals:<p> {% for dr in object_list %} {{dr.name}}</p> {% endfor %} {% endblock %}
访问 /myapp/dreamreals/ 将产生如下页面 −