Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django:django-tables2 DetailView CBV赢得';不显示单个对象_Python_Django_Django Tables2_Detailview - Fatal编程技术网

Python django:django-tables2 DetailView CBV赢得';不显示单个对象

Python django:django-tables2 DetailView CBV赢得';不显示单个对象,python,django,django-tables2,detailview,Python,Django,Django Tables2,Detailview,我有一张桌子 import django_tables2 as tables from .models import Account from django_tables2.utils import A # alias for Accessor class AccountTable(tables.Table): nickname = tables.LinkColumn('accounts:detail', args=[A('pk')]) class Meta:

我有一张桌子

import django_tables2 as tables
from .models import Account
from django_tables2.utils import A  # alias for Accessor


class AccountTable(tables.Table):
    nickname = tables.LinkColumn('accounts:detail', args=[A('pk')])

    class Meta:
        model = Account
        attrs = {'class': 'table table-striped table-hover'}
        exclude = ("created", "modified", "destination")
一种观点:

class DetailView(SingleTableMixin, generic.DetailView):

    template_name = 'accounts/account_detail.html'
    context_table_name = 'table'
    model = Account
    table_class = AccountTable
    context_object_name = object

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(DetailView, self).dispatch(*args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(object=self.object)
        context['title'] = 'Account Detail'
        context['pk'] = self.kwargs.get(self.pk_url_kwarg, None)
        return context
和一个模板:

<!DOCTYPE html>
{% extends "accounts/base.html" %}
{% load django_tables2 %}
{% load render_table from django_tables2 %}
<title>Account Detail</title>

{% block content %}
<br>
<br>
<h1>{{ object.id }}  :  {{object.nickname}}</h1>

<div> 
{% render_table table %}    
</div>
{% endblock %}

{%extends“accounts/base.html”%}
{%load django_表2%}
{%load render_Tables from django_Tables 2%}
账户明细
{%block content%}


{{object.id}}:{{object.昵称} {%render_table%} {%endblock%}
它正确地获取对象pk和所有内容,但不只是发送一个对象来填充表。我知道它得到了对象,因为object.id和object.昵称都显示正确。我知道有可能只显示一个特定对象,因为在同一个项目中,我有另一个应用程序,如果您单击指向DetailView的链接(我借用该模板用我的帐户模型重新创建),它只显示一个对象。但它将只显示所有对象的表


如有必要,我可以提供请求数据。我可以向您保证,我已经在模板上下文中看到了对象,事实上它必须是,否则object.id将无法工作。django-tables2的诀窍是什么?显然我已经做过一次了

您可以覆盖视图的
get\u table\u data
方法,并返回要显示的对象列表

在这种情况下,您需要一个只包含一项的列表,即
详细视图中的对象

def get_table_data(self):
    return [self.object]

当我尝试在没有括号的情况下使用时,我得到了这个错误:“数据必须是QuerySet(have count and order_by)或support list(data)--Account两者都没有。”然后我看到您有一个括号,它工作了。谢谢括号是必需的,因为django-tables2希望
get_table_data
返回一个类似queryset或list的iterable,而不是单个对象。