我有一个视图定义(试图)将模型输出为表格.这是我到目前为止的内容:
I have a view definition that (attempts to) outputs a model as a table. This is what I have so far:
def output_table(request): output = My_Model() return render_to_response('outputtable.html', {'output': output})这是outputtable.html的HTML:
Here's the HTML for outputtable.html:
<html> <table> {{ output.as_table }} </table> </html>我做错了什么?它不起作用.现在,它正确传递了模型,因为如果我将 My_Model()更改为 My_Model.objects.all(),然后将其输出为 {{output}} 然后它显示了我在Django shell中看到的内容.
What am I doing wrong? It doesn't work. Now, it's passing the model correctly, because if I change My_Model() to My_Model.objects.all() and then output it as simply {{ output }} then it shows me what I would see in the Django shell.
推荐答案因此,您需要执行以下操作:
So what you need to do is the following:
1)添加
导入ModelForm 到您的models.py
from django.forms import ModelForm to your models.py
2)添加
class My_Model_Form(ModelForm): class Meta: model = My_Model3)在您的views.py中,将 output = My_Model()更改为 output = My_Model_Form()
3) in your views.py, change output = My_Model() to output = My_Model_Form()
现在您已经准备就绪.因此,诀窍是从原始模型继承您的Form.
Now you are all set. So the trick is to inherit your Form from your original model.
更多推荐
将Django模型输出为表格
发布评论