在Django中查看扩展的DetailView时出错

时间:2017-05-06 18:00:23

标签: django django-views

我正在扩展DetailView以在对象的详细视图中显示项目列表。即使没有错误,也不会显示输出。

这是代码清单。 在扩展视图中

class Developer_detail(DetailView):
    model = Developer
    template_name = 'en/public/developer_detail.html'

    def get_context_data(self, **kwargs):
        def get_context_data(self, **kwargs):
            context = super(Developer_detail, self).get_context_data(**kwargs)
            tasks_dev = Task.oblects.filter(developer=self.object)
            context['tasks_dev'] = tasks_dev

            return context

在urls.py

url(r'developer-detail_(?P<pk>\d+)/$', Developer_detail.as_view(), name='developer_detail'),

在模板中

{% extends 'en/public/base.html' %}
{% block title_html %}
    Developer Details
{% endblock %}

{% block h1 %}
    Developer Details - {{ object.name }}
{% endblock %}

{% block article_content %}
    <h4>{{ object.name }}</h4>
    <span>Login: {{ object.login }}</span><br />
    <span>Email: {{ object.email }}</span>
    <h3>Tasks</h3>
    <table>
        {% for task in tasks_dev %}
        <tr>
            <td>{{ task.title }}</td>
            <td>{{ task.importance }}</td>
            <td>{{ task.project }}</td>
        </tr>
        {% endfor %}
    </table>
    <p>
        {{ object.description }}
    </p>
{% endblock %}

以上模板渲染未显示对象 tasks_dev 数据。

1 个答案:

答案 0 :(得分:1)

您在get_context_data方法中声明了get_context_data。它应该是:

    def get_context_data(self, **kwargs):
        # Only one declaration, otherwise None will be returned by the function
        context = super(Developer_detail, self).get_context_data(**kwargs)
        tasks_dev = Task.oblects.filter(developer=self.object)
        context['tasks_dev'] = tasks_dev

        return context