python - 模型以字典形式查看,然后将其呈现为 html 表

标签 python html django django-models django-views

我正在学习 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()但它说

General has no objects

所以我无法手动制作字典

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>

有人能发现我做错了什么吗?我在整个互联网上潜伏了三天来寻找答案。

最佳答案

类是一个“蓝图”,可以用来“实例化”不同的“对象”。它仅指定不同的属性和方法。但在模板中我们需要显示真实对象的列表。

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)

关于python - 模型以字典形式查看,然后将其呈现为 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59654611/

相关文章:

python - 如何在Python框架Scrapy中从页面解析RSS链接(获取RSS的url)?

python - Azure 语音到文本 0x38 (SPXERR_AUDIO_SYS_LIBRARY_NOT_FOUND)

javascript - 用于在选择时更改底部边框颜色的 anchor 标记

html - 连续动态对齐缩略图

Django 带过滤器的聚合查询

mysql - 在 mysql 的 django 模型中,当对 int(1) 使用 IntegerField 时我们会丢失信息吗

python - 如何执行表达式?

python - for 循环后无法追加 numpy 数组吗?

html - 粘性 "footer"到 div 底部而不是页面

python - 在 Django 上测试表单