如何在 python 中将查询结果映射到 HTML 表?

发布于 2025-01-21 01:58:56 字数 2145 浏览 2 评论 0原文

我有两个表:Driver 和 Car

from django.db import models

# Create your models here.

class Driver(models.Model):
    name = models.TextField()
    license = models.TextField()

class Car(models.Model):
    make = models.TextField()
    model = models.TextField()
    year = models.IntegerField()
    vin = models.TextField()
    owner = models.ForeignKey("Driver", on_delete=models.SET_NULL, null=True)

这是表内的数据:

INSERT INTO car_driver (name, license)
VALUES ('John Smith', 'ZX1092931i'),
        ('Karen Doe', 'KOanksndan');


INSERT INTO car_car (make, model, year, vin, owner_id)
VALUES
    ('Mercedes', 'Legend', 2009, '912asdasda12', 1),
    ('Nissan', 'Altima', 2002, 'a9a98aa7a772', 1),
    ('Lada', 'Kalina', 1987, 'SU91991', 2),
    ('BMW', 'Challenger', 2013, 'BM91818X', 2);

无论我执行什么类型的查询,我都想显示一个空的 html 表,并根据查询填充结果

from django.shortcuts import render
from car.models import Car, Driver
# Create your views here.


def car_detail(request, pk):
    owner_obj = Driver.objects.get(pk=pk)
    car_objs = Car.objects.filter(owner_id=owner_obj.id)
    context = {
        "vehicles": car_objs,
        "drivers": owner_obj,
    }

    return render(request, "v2.html", context)

这里是 html:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Contacts</title>
        <link rel="stylesheet" href="https://unpkg.com/[email protected]/css/tachyons.min.css"/>
    </head>

    <body>
        <h1>V2</h1>

        <table>
            {% block page_content %}
            <tr>
                <th></th>
            </tr>
            {% endblock %}
        </table>
        

    </body>

</html>

我来自 Node/Express,我在那里的方式是使用 array.map(),但是如何在 python/django 中执行此操作?我不知道如何使用 {% ... %}

I have two tables : Driver and Car

from django.db import models

# Create your models here.

class Driver(models.Model):
    name = models.TextField()
    license = models.TextField()

class Car(models.Model):
    make = models.TextField()
    model = models.TextField()
    year = models.IntegerField()
    vin = models.TextField()
    owner = models.ForeignKey("Driver", on_delete=models.SET_NULL, null=True)

And here is the data inside the tables:

INSERT INTO car_driver (name, license)
VALUES ('John Smith', 'ZX1092931i'),
        ('Karen Doe', 'KOanksndan');


INSERT INTO car_car (make, model, year, vin, owner_id)
VALUES
    ('Mercedes', 'Legend', 2009, '912asdasda12', 1),
    ('Nissan', 'Altima', 2002, 'a9a98aa7a772', 1),
    ('Lada', 'Kalina', 1987, 'SU91991', 2),
    ('BMW', 'Challenger', 2013, 'BM91818X', 2);

It doesn't matter what type of query I do, I would like to display an empty html table, and depending on the query the results get populated

from django.shortcuts import render
from car.models import Car, Driver
# Create your views here.


def car_detail(request, pk):
    owner_obj = Driver.objects.get(pk=pk)
    car_objs = Car.objects.filter(owner_id=owner_obj.id)
    context = {
        "vehicles": car_objs,
        "drivers": owner_obj,
    }

    return render(request, "v2.html", context)

And here is the html:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Contacts</title>
        <link rel="stylesheet" href="https://unpkg.com/[email protected]/css/tachyons.min.css"/>
    </head>

    <body>
        <h1>V2</h1>

        <table>
            {% block page_content %}
            <tr>
                <th></th>
            </tr>
            {% endblock %}
        </table>
        

    </body>

</html>

I'm coming from Node/Express, the way I would do it there is with an array.map(), but how do go about doing this in python/django? I'm not sure how to use {% ... %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2025-01-28 01:58:56

根据您的观点,上下文对象中的“车辆”将是与“司机”对应的查询集。由于“司机”将是一个单一的对象,我相信,您正在寻找基于上下文对象的“车辆”创建一个表。您可以在包含所有列名称的上下文对象中再创建一个键值对。

        <table>
            <thead>
               <tr>
                {% for column in column_names %}
                    <td>{column}</td>
                {% endfor %}
               </tr>
            </thead>
            <tbody>
                {% for car in car_obj %}
                    <tr>
                        <td>{car.make}</td>
                        <td>{car.model}</td>
                        <td>{car.year}</td>
                        <td>{car.vin}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>

As per your views, 'vehicles' in context object will be the queryset corresponding to the 'driver'. Since 'driver' will be a single object, I believe, you are looking to create a table based on 'vehicles' of context object. You can create one more key-value pair in context object that includes all column names.

        <table>
            <thead>
               <tr>
                {% for column in column_names %}
                    <td>{column}</td>
                {% endfor %}
               </tr>
            </thead>
            <tbody>
                {% for car in car_obj %}
                    <tr>
                        <td>{car.make}</td>
                        <td>{car.model}</td>
                        <td>{car.year}</td>
                        <td>{car.vin}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
贪了杯 2025-01-28 01:58:56

在您的桌子上做类似的事情:

{% for item in context.vehicles %}
   <tr>{{ item.make }}</tr> 
{% endfor %}

Do something like this in your table:

{% for item in context.vehicles %}
   <tr>{{ item.make }}</tr> 
{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文