【问题标题】:Model to view as dictionary then render it as a html table模型以字典形式查看,然后将其呈现为 html 表
【发布时间】: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


    【解决方案1】:

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

    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)
    

    【讨论】:

      【解决方案2】:

      你也可以使用Django的基于类的List View

      将所有常规数据对象作为上下文对象推送到模板中。它还将为您处理渲染和获取/发布

      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
      

      你可以像这样在你的模板中引用

       {% for item in dto %}
          <td>{{ item.<whatever> }}</td>
       {% endfor %}
      

      【讨论】:

        猜你喜欢
        • 2021-06-11
        • 2011-08-29
        • 1970-01-01
        • 2020-06-30
        • 1970-01-01
        • 2017-06-10
        • 2016-10-22
        • 2021-11-16
        • 1970-01-01
        相关资源
        最近更新 更多