如何在 python 中将查询结果映射到 HTML 表?
我有两个表: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的观点,上下文对象中的“车辆”将是与“司机”对应的查询集。由于“司机”将是一个单一的对象,我相信,您正在寻找基于上下文对象的“车辆”创建一个表。您可以在包含所有列名称的上下文对象中再创建一个键值对。
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.
在您的桌子上做类似的事情:
Do something like this in your table: