[英]Model to view as dictionary then render it as a html table
Im learning Django and get stuck with this problem.我正在学习 Django 并遇到了这个问题。 I need to list "users" with their respective data to an html list.
我需要将“用户”及其各自的数据列出到 html 列表中。 I tried but there is no list generated.
我试过了,但没有生成列表。 Already have my db populated with 100 lines.
我的数据库已经填充了 100 行。
This is my models.py这是我的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
This is my views.py, already tried return render_to_response
, and tried to use General.objects.all()/General.objects.get()
but it says这是我的 views.py,已经尝试过
return render_to_response
,并尝试使用General.objects.all()/General.objects.get()
但它说
General has no objects
将军没有对象
so i cant make the dictionary manually所以我不能手动制作字典
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)
And this is my HTML:这是我的 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>
Someone can spot what im doing wrong?I was been 3 days lurking the whole internet to find answers.有人可以发现我做错了什么吗?我已经在整个互联网上潜伏了 3 天寻找答案。
A class is a "blueprint" that can be used to "instantiate" different "objects".类是可用于“实例化”不同“对象”的“蓝图”。 It only specifies the different properties and methods.
它仅指定不同的属性和方法。 But in template we need to show a list of real objects.
但是在模板中,我们需要显示真实对象的列表。
def VisualizadorGeneral(request):
objects_list = General.objects.all() # here we are retrieving real instances
list_of_object_dicts = [model_to_dict(obj) for obj in objects_list] # and in this row we are applying function model_to_dict for each object
context_data = {"listaGeneralDic: list_of_object_dicts} # context should be a dict with keys and values, after announcing "listaGeneralDic" in context we will be able to call it in template
return render(request, "VisualizadorGeneral.html", context=context_data)
You can also use Django's class-based List View您还可以使用 Django 的基于类的列表视图
to have all the General data objects shoved to the template as a context object for you.将所有通用数据对象作为上下文对象推送到模板中。 It will also take care of rendering and get/post for you
它还将为您处理渲染和获取/发布
from django.views.generic import ListView
class VisualizadorGeneral(ListView)
template_name = '<your_template_name>'
model = General
context_object_name = 'dto'
# this is the name you will refer to the object in the template
you can refer to in your template like this你可以像这样在你的模板中引用
{% for item in dto %}
<td>{{ item.<whatever> }}</td>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.