使用 Django 模板打印对象的所有属性

Print all attributes from an object with Django templates

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

我有许多具有许多属性的模型,并且每个模型都有一个 window 和一个模板。 这个想法是在每个文件中显示一个 table 及其所有内容,并在文件 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>

我的想法是通过访问 class 的所有属性来自动化 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

好像不识别name作为class的属性,一定是因为是字符串吧?

我看到这个问题,但我不明白

对自动执行此操作有帮助吗?非常感谢

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

此 returns 所有可用值的列表:

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>

一切顺利