输出Django模型作为表

Rez*_*nor 6 django model html-table

我有一个视图定义(尝试)将模型输出为表.这是我到目前为止:

def output_table(request):
    output = My_Model()
    return render_to_response('outputtable.html', {'output': output})
Run Code Online (Sandbox Code Playgroud)

这是outputtable.html的HTML:

<html>
<table>
    {{ output.as_table }}
</table>
</html>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?它不起作用.现在,它正确地传递模型,因为如果我将My_Model()更改为My_Model.objects.all()然后将其输出为简单的{{output}},那么它会显示我将在Django shell中看到的内容.

ste*_*anw 0

as_table模型实例 ( MyModel()) 或查询集 ( ) 上没有方法MyModel.objects.all()。您可能已经看到了表单as_table方法。你那里没有表格。as_table

如果您想将模型实例/查询集打印为表格,则必须自己设计。