【发布时间】:2020-04-26 12:08:44
【问题描述】:
我正在学习 Django,但遇到了这个问题。 我需要将“用户”及其各自的数据列出到 html 列表中。 我试过了,但没有生成列表。我的数据库已经填充了 100 行。
这是我的models.py
from django.db import models
# Create your models here.
class General(models.Model):
id = models.CharField(max_length=128, primary_key=True)
name= models.CharField(max_length=128)
lastname= models.CharField(max_length=128)
state= models.CharField(max_length=128)
asd= models.CharField(max_length=128)
spc= models.CharField(max_length=128)
dot = models.CharField(max_length=128, blank=True)
def __str__(self):
return self.name
这是我的views.py,已经尝试过return render_to_response,并尝试使用General.objects.all()/General.objects.get(),但它说
一般没有对象
所以我不能手动制作字典
from django.shortcuts import render
from Myapp.models import General
from django.forms.models import model_to_dict
# Create your views here.
def VisualizadorGeneral(request):
listaGeneralDic = model_to_dict(General)
return render(request, "VisualizadorGeneral.html", context=listaGeneralDic)
这是我的 HTML:
<div class="container">
{% if listaGeneralDic %}
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>lastname</th>
<th>state</th>
<th>asd</th>
<th>spc</th>
<th>dot</th>
</tr>
</thead>
<tbody>
{% for dato in listaGeneralDic %}
<tr>
<td scope="row">{{ dato.id }}</td>
<td>{{ dato.name }}</td>
<td>{{ dato.lastname }}</td>
<td>{{ dato.state }}</td>
<td>{{ dato.asd }}</td>
<td>{{ dato.spc }}</td>
<td>{{ dato.dot }}</td>
</tr>
{% endfor %}
</tbody>
{% else %}
<p>Nothing to see</p>
{% endif %}
</table>
</div>
有人能看出我做错了什么吗?我在整个互联网上潜伏了 3 天以寻找答案。
【问题讨论】:
标签: python html django django-models django-views