【问题标题】:Print all attributes from an object with Django templates使用 Django 模板打印对象的所有属性
【发布时间】:2021-10-09 11:29:50
【问题描述】:

我是一名新手程序员,我正在自学python和Django。我卡在了我的应用程序的一部分中:

我有许多具有许多属性的模型,并且每个模型都有一个窗口和一个模板。 我们的想法是在每个文件中显示一个包含所有内容的表,并在文件 view.py 中使用 sql 查询

objects.all ()

我的代码:

MODELS.PY

from django.db import models

class MFuel(models.Model):
    marca = models.TextField(db_column='Marca', blank=True, null=True)  # Field name made lowercase.
    modelo = models.TextField(db_column='Modelo', blank=True, null=True)  # Field name made lowercase.
    potencia_electrica_kw = models.BigIntegerField(db_column='Potencia_electrica_kW', blank=True, null=True)  # Field name made lowercase.
    fecha_oferta = models.DateTimeField(db_column='Fecha_oferta', blank=True, null=True)  # Field name made lowercase.
    pais_proyecto = models.TextField(db_column='Pais_Proyecto', blank=True, null=True)  # Field name made lowercase.
    proyecto = models.TextField(db_column='Proyecto', blank=True, null=True)  # Field name made lowercase.
    uds = models.FloatField(db_column='Uds', blank=True, null=True)  # Field name made lowercase.
    precio_eur = models.FloatField(db_column='Precio_EUR', blank=True, null=True)  # Field name made lowercase.
    precio_usd = models.FloatField(db_column='Precio_USD', blank=True, null=True)  # Field name made lowercase.
    precio_unitario_eur = models.FloatField(db_column='Precio_Unitario_EUR', blank=True, null=True)  # Field name made lowercase.
    ratio_eur_kw = models.FloatField(db_column='Ratio_eur_KW', blank=True, null=True)  # Field name made lowercase.
    largo_mm = models.FloatField(db_column='Largo_mm', blank=True, null=True)  # Field name made lowercase.
    ancho_mm = models.FloatField(db_column='Ancho_mm', blank=True, null=True)  # Field name made lowercase.
    alto_mm = models.FloatField(db_column='Alto_mm', blank=True, null=True)  # Field name made lowercase.
    peso_kg = models.FloatField(db_column='Peso_kg', blank=True, null=True)  # Field name made lowercase.
    presupuesto = models.TextField(db_column='Presupuesto', blank=True, null=True)  # Field name made lowercase.
    esp_tecnicas = models.TextField(db_column='Esp_Tecnicas', blank=True, null=True)  # Field name made lowercase.
    observaciones = models.FloatField(db_column='OBSERVACIONES', blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'm_fuel'

VIEWS.PY

def m_fuel(request):
    fuel = MFuel.objects.all()

    # QuerySet
    myFilter = Filter(request.GET, queryset=fuel)
    fuel = myFilter.qs

    total = fuel.count()

    attr = MFuel.__doc__

    inici = attr.index("(") + 1
    fi = attr.index(")")
    nom_columnes = attr[inici:fi]
    nom_columnes = str(nom_columnes)
    nom_columnes = nom_columnes.split(",")

    dict = {
        "fuel": fuel,
        "filter": myFilter,
        "motores": motores_list,
        "total": total,
        "nom_columnes": nom_columnes,
    }

    return render(request, "motores/fuel.html", dict)

TEMPLATE.HTML

<table class="table_info">
        <tr>
            {% for nom in nom_columnes %}
            <th>{{nom}}</th>
            {% endfor %}
        </tr>

        {% for i in fuel %}
        <tr>
            <td>{{i.id}}</td>
            <td>{{i.marca}}</td>
            <td>{{i.modelo}}</td>
            <td>{{i.potencia_electrica_kw}}</td>
            <td>{{i.fecha_oferta}}</td>
            <td>{{i.pais_proyecto}}</td>
            <td>{{i.proyecto}}</td>
            <td>{{i.uds}}</td>
            <td>{{i.precio_eur}}</td>
            <td>{{i.largo_mm}}</td>
            <td>{{i.ancho_mm}}</td>
            <td>{{i.alto_mm}}</td>
            <td>{{i.peso_kg}}</td>
            <td>{{i.presupuesto}}</td>
        </tr>
        {% endfor %}
    </table>

我的想法是通过访问类的所有属性来自动化表列。 我尝试提取属性名称列表

attr = MFuel .__ doc__

并按如下方式打印它们:

<table class="table_info">
        <tr>
            {% for nom in nom_columnes %}
            <th>{{nom}}</th>
            {% endfor %}
        </tr>

        {% for i in fuel %}
        <tr>
            {% for nom in nom_columnes %}
            <td>{{i.nom}}</td>
            {% endfor %}
        </tr>
        {% endfor %}
    </table>

但这什么也没显示: capture that this code shows

好像没有把名字识别为类的属性,应该是因为是字符串吧?

我看到了这个问题,但我不明白 Django - print all objects attribute values

对自动化有什么帮助吗?非常感谢

【问题讨论】:

    标签: python django django-views django-templates attributes


    【解决方案1】:

    我已经能够解决我的问题,我分享它以防有人需要它

    这将返回所有可用值的列表:

    fuel = fuel.values()
    

    并打印出来:

    <table class="table_info">
        <tr>
            {% for nom in nom_columnes %}
            <th>{{nom}}</th>
            {% endfor %}
        </tr>
        {% for i in fuel %}
        <tr>
            {% for key, value in i.items %}
                <td>{{value}}</td>
            {% endfor %}
        </tr>
        {% endfor %}
    </table>
    

    一切都好

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 2018-12-28
      • 2011-03-03
      • 2019-08-13
      • 2010-10-25
      • 1970-01-01
      • 2017-09-03
      • 2013-07-11
      • 2018-04-23
      相关资源
      最近更新 更多