【问题标题】:Django 1.7 generic viewsDjango 1.7 通用视图
【发布时间】:2015-01-20 23:10:55
【问题描述】:

我目前正在阅读 Django 的官方教程,但在尝试理解通用视图的实际工作原理时遇到了麻烦。

来自官方文档的源码:

class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'


class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

detail.html

<h1>{{ question.question_text }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

结果.html

<h1>{{ question.question_text }}</h1>

<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>

<a href="{% url 'polls:detail' question.id %}">Vote again?</a>

现在,我了解到 ListViewDetailView 是 Django 提供的默认通用视图。

DetailViewResultsView 如何在detail.htmlresult.html 中生成question 上下文变量?另外,DetailView 生成的detail.html 中的error_message 上下文变量是如何产生的?

【问题讨论】:

    标签: python django django-1.7


    【解决方案1】:

    question 对象以通用视图的model 属性命名(在本例中为Question)。详细视图会自动包含这样的对象。

    如果您询问如何选择特定的 question 对象,则默认情况下,详细视图必须从 URL 传递一个 pk 值,然后用于查找该对象。 polls/urls.py 中的相关代码如下:

    url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
    

    另一方面,error_message 不包含在通用视图中。它仅在示例中用于非通用视图,因为它明确地将其包含在上下文中:

    return render(request, 'polls/detail.html', {
        'question': p,
        'error_message': "You didn't select a choice.",
    })
    

    【讨论】:

      【解决方案2】:

      DetailView 有一个context_object_name 参数,可以用来设置模板中对象的名称,但如果不设置,get_context_object_name 方法会这样做:

      def get_context_object_name(self, obj):
          """
          Get the name to use for the object.
          """
          if self.context_object_name:
              return self.context_object_name
          elif isinstance(obj, models.Model):
              return obj._meta.model_name
          else:
              return None
      

      它使用模型的名称。然后SingleObjectMixinget_context_data 将该标识符放入上下文中。在任何情况下,您也可以使用object 标识符访问对象。

      我相信您可以在 Django 文档中阅读所有这些内容,但这里有 a nice page where you can explore class based views

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-02
        相关资源
        最近更新 更多