Django 简明教程

Django - Generic Views

在某些情况下,编写视图(如前面看到的那样)确实很繁琐。想象您需要一个静态页面或列表页面。Django 提供了一种设置这些简易视图的简单方法,该方法称为通用视图。

In some cases, writing views, as we have seen earlier is really heavy. Imagine you need a static page or a listing page. Django offers an easy way to set those simple views that is called generic views.

与经典视图不同,通用视图是类而不是函数。Django 在 django.views.generic 中提供了一组通用视图类,而每个通用视图都是这些类之一或从其中一个类继承的类。

Unlike classic views, generic views are classes not functions. Django offers a set of classes for generic views in django.views.generic, and every generic view is one of those classes or a class that inherits from one of them.

共有 10+ 个通用类:

There are 10+ generic classes −

>>> 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']

您可以将其用于您的通用视图。让我们看一些示例以了解其工作原理。

This you can use for your generic view. Let’s look at some example to see how it works.

Static Pages

让我们从“static.html”模板发布一个静态页面。

Let’s publish a static page from the “static.html” template.

我们的 static.html:

Our static.html −

<html>
   <body>
      This is a static page!!!
   </body>
</html>

如果我们按照之前学习的方式进行操作,则必须将 myapp/views.py 更改为:

If we did that the way we learned before, we would have to change the myapp/views.py to be −

from django.shortcuts import render

def static(request):
   return render(request, 'static.html', {})

并将 myapp/urls.py 更改为:

and myapp/urls.py to be −

from django.conf.urls import patterns, url

urlpatterns = patterns("myapp.views", url(r'^static/', 'static', name = 'static'),)

最好的方法是使用通用视图。为此,我们的 myapp/views.py 将变为:

The best way is to use generic views. For that, our myapp/views.py will become −

from django.views.generic import TemplateView

class StaticView(TemplateView):
   template_name = "static.html"

而我们的 myapp/urls.py 将变为:

And our myapp/urls.py we will be −

from myapp.views import StaticView
from django.conf.urls import patterns

urlpatterns = patterns("myapp.views", (r'^static/$', StaticView.as_view()),)

在访问 /myapp/static 时,您会获得:

When accessing /myapp/static you get −

static page

对于相同的结果,我们还可以执行以下操作:

For the same result we can also, do the following −

  1. No change in the views.py

  2. Change the url.py file to be −

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 文件。

As you can see, you just need to change the url.py file in the second method.

List and Display Data from DB

我们将列出 Dreamreal 模型中的所有条目。使用 ListView 通用视图类可以轻松做到这一点。编辑 url.py 文件并将其更新为 −

We are going to list all entries in our Dreamreal model. Doing so is made easy by using the ListView generic view class. Edit the url.py file and update it as −

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。如果您想要自己命名,则需要向 as_view 方法添加 context_object_name 参数。然后,url.py 将变为 −

Important to note at this point is that the variable pass by the generic view to the template is object_list. If you want to name it yourself, you will need to add a context_object_name argument to the as_view method. Then the url.py will become −

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” ,)

然后,关联的模板将为 −

The associated template will then be −

{% extends "main_template.html" %}
{% block content %}
Dreamreals:<p>
{% for dr in object_list %}
{{dr.name}}</p>
{% endfor %}
{% endblock %}

访问 /myapp/dreamreals/ 将生成以下页面 −

Accessing /myapp/dreamreals/ will produce the following page −

list display data from db