在某些情况下编写视图代码,正如我们所看到前面工作真的很繁重。想象一下,只需要一个静态页面或列表页。 django还提供了一种简单的方法来设置这些简单的视图被称为泛型视图。
不同于传统的视图,一般的视图是一个类但没有函数。django还提供了一组类 django.views.generic 泛型视图,以及每一个普通视图是这些类或从它们中的一个类继承的。
有10 泛型类−
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # 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 : https://www.elefans.com/biancheng/ # date : 2020-08-08 this is a static page!!!
如果我们这样做,按以前学过的方式,我们将不得不改变 myapp/views.py −
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # 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 : https://www.elefans.com/biancheng/ # date : 2020-08-08 from django.conf.urls import patterns, url urlpatterns = patterns("myapp.views", ,)
最好的办法就是使用泛型视图。对于这一点,我们的 myapp/views.py 将变成为 −
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # 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 : https://www.elefans.com/biancheng/ # 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 : https://www.elefans.com/biancheng/ # date : 2020-08-08 from django.views.generic import templateview from django.conf.urls import patterns, url urlpatterns = patterns("myapp.views", url(/uploads/image/bdr'^static/',templateview.as_view(template_name-nbsp;=-nbsp;'static.html')),)
正如你所看到的,只需要改变url.py文件中的第二个方法。
我们要列出所有条目在dreamreal模型。这样使用listview泛型视图类变得容易。编辑url.py文件,并对其进行更新 -
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 from django.views.generic import listview from django.conf.urls import patterns, url urlpatterns = patterns( "myapp.views", ), )
重要的是要注意,在这一点上是变量通由泛型视图到模板为object_list。如果你想自己的名字,将需要一个 context_object_name 参数添加到as_view方法。然后 url.py 成为 -
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 from django.views.generic import listview from django.conf.urls import patterns, url urlpatterns = patterns("myapp.views", ), model = dreamreal, context_object_name = ”dreamreals_objects” ,)
然后关联的模板将成为 −
# filename : example.py # 九游会下载 copyright : 2020 by nhooo # author by : https://www.elefans.com/biancheng/ # date : 2020-08-08 {% extends "main_template.html" %} {% block content %} dreamreals:{% for dr in object_list %} {{dr.name}}
{% endfor %} {% endblock %}
访问 /myapp/dreamreals/ 将产生如下页面 −