Django使用for循环生成引导选项卡
我的web应用程序中有一个包含多个表的页面,而不是将它们放在单独的页面或一个长列表中,我希望使用选项卡,并且我需要能够根据对象的数量生成这些选项卡 我很确定这只是模板的一个问题,但我会包括所有其他文件以防万一 以下是与问题相关的模型Django使用for循环生成引导选项卡,django,twitter-bootstrap,for-loop,django-templates,Django,Twitter Bootstrap,For Loop,Django Templates,我的web应用程序中有一个包含多个表的页面,而不是将它们放在单独的页面或一个长列表中,我希望使用选项卡,并且我需要能够根据对象的数量生成这些选项卡 我很确定这只是模板的一个问题,但我会包括所有其他文件以防万一 以下是与问题相关的模型 class Exercises(models.Model): exercise = models.CharField(max_length=45) evolution = models.CharField(max_length=8) star
class Exercises(models.Model):
exercise = models.CharField(max_length=45)
evolution = models.CharField(max_length=8)
start_date = models.DateTimeField(blank=True, null=True)
end_date = models.DateTimeField(blank=True, null=True)
logo = models.TextField(blank=True, null=True)
class Meta:
constraints = [
models.UniqueConstraint(
fields=['exercise', 'evolution'], name='exercise_evolution_UNIQUE')
]
def __str__(self):
return f"Exercise: {self.exercise}\t Evolution: {self.evolution}"
class Units(models.Model):
uic = models.CharField(max_length=10)
unit_name = models.CharField(unique=True, max_length=45)
mcc = models.CharField(max_length=5, blank=True, null=True)
ruc = models.CharField(max_length=10, blank=True, null=True)
personnel = models.IntegerField(blank=True, null=True)
class Meta:
constraints = [
models.UniqueConstraint(
fields=['uic', 'mcc', 'ruc'], name='uic_mcc_ruc_UNIQUE')
]
def __str__(self):
return f"{self.unit_name}"
def get_uic(self):
return f'{self.uic}'
class ExercisePersonnel(models.Model):
exercise = models.ForeignKey('Exercises', models.CASCADE)
unit = models.ForeignKey('Units', models.CASCADE)
location = models.ForeignKey('Locations', models.RESTRICT)
quantity = models.IntegerField()
def __str__(self):
return f'{self.unit}'
class UnitEdl(models.Model):
unit = models.ForeignKey('Units', models.CASCADE)
equipment = models.ForeignKey('Equipment', models.CASCADE)
quantity = models.IntegerField()
class Meta:
constraints = [
models.UniqueConstraint(
fields=['unit', 'equipment'], name='unit_equipment_UNIQUE')
我有一些函数,它们使用数据库对象来创建显示在表中的其他数据
函数.py
def get_class_one_chow(pax=1, meals_per_day=2):
cases_per_day = pax * meals_per_day / 12
pallets = cases_per_day / 48
short_tons = pallets * 1098 / 2000
twenty_ft_teus = pallets / 16
ugrs_pallets = (pax * 2) / 50 / 8
mres_pallets = pax / 12 / 48
ugrs_short_tons = (ugrs_pallets * 1071 / 2000) + (mres_pallets * 1098 / 2000)
ugr_twenty_ft_teus = (ugrs_pallets + mres_pallets) / 16
return {"cases":round(cases_per_day,1),
"pallets":round(pallets,1),
"short_tons":round(short_tons,1),
"twenty_ft_teus":round(twenty_ft_teus,1),
"ugrs_pallets":round(ugrs_pallets,1),
"mres_pallets":round(mres_pallets,1),
"ugrs_short_tons":round(ugrs_short_tons,1),
"ugr_twenty_ft_teus":round(ugr_twenty_ft_teus,1)
}
def get_class_one_water_bottled(pax=1, planning_factor=2):
min_gallons = pax * planning_factor
min_bottles = (min_gallons * 3.785) * 2
min_cases = min_bottles / 24
min_pallets = min_cases / 72
min_short_tons = min_pallets * 2182 / 2000
twenty_ft_teus = min_pallets / 16
return {"gallons":round(min_gallons,1),
"bottles":round(min_bottles,1),
"cases":round(min_cases,1),
"pallets":round(min_pallets,1),
"short_tons":round(min_short_tons,1),
"twenty_ft_teus":round(twenty_ft_teus,1)
}
def get_class_one_water_bulk(pax=1, planning_factor=1):
min_gallons = pax * planning_factor
min_short_tons = (min_gallons * 8.34) / 2000
return {"gallons":round(min_gallons,1),
"short_tons":round(min_short_tons,1)
}
这在视图中使用
def exercise_detail(request, pk, *args, **kwargs):
ex_name = Exercises.objects.get(pk=pk)
ex_personnel = ExercisePersonnel.objects.filter(exercise_id=pk)
planners = ExercisePermissions.objects.filter(exercise=pk)
user_list = MlptUsers.objects.order_by('email')
unit_list_planner = Units.objects.order_by('unit_name')
unit_list, chow_list, bottled_water_list, bulk_water_list = {}, {}, {}, {}
# generate dictionaries so you can parse in template
for unit in ex_personnel:
unit_list[f"{unit}"] = unit.quantity
chow_list[f"{unit}"] = get_class_one_chow(pax=unit.quantity)
bottled_water_list[f"{unit}"] = get_class_one_water_bottled(
pax=unit.quantity)
bulk_water_list[f"{unit}"] = get_class_one_water_bulk(
pax=unit.quantity)
context = {
'ex_name': ex_name,
'chow_list': chow_list,
'bottled_water_list': bottled_water_list,
'bulk_water_list': bulk_water_list,
'unit_list': unit_list,
'planners': planners,
'user_list': user_list,
'unit_list_planner': unit_list_planner,
}
return render(request, 'exercise/exercise_detail.html', context)
但是,我无法使选项卡在模板中正常工作
我可以创建表并使用for循环为字典“unit_list”中的每个键创建一行新行,该字典显示所有信息,但我不希望它放在一个地方,就像这样
规划数据
数字是每天的
{键为%,单位为\u list.items%}
-
{{key}}
{%endfor%}
I类-水(瓶装)
单位
女孩
BTL
案例
PLT
石子
20标准箱
{%为键,瓶装水中的值\u list.items%}
{{key}}
{{value.gallons}}
{{value.labels}}
{{value.cases}
{{value.pallets}
{{value.short_tons}
{{value.two_ft_teus}
{%endfor%}
I类-水(散装)
单位
女孩
石子
{%表示键,值在bulk\u water\u list.items%}
{{key}}
{{value.gallons}}
{{value.short_tons}
{%endfor%}
第一类-周
单位
案例
PLT
石子
20标准箱
UGR PLT
磁共振光电倍增管
乌尔斯顿
UGR 20标准箱
{键为%,值在chow_list.items%}
{{key}}
{{value.cases}
{{value.pallets}
{{value.short_tons}
{{value.two_ft_teus}
{{value.ugrs_pallets}
{{value.mres_pallets}
{{value.ugrs_short_tons}
{{value.ugr_二十英尺标准箱}
{%endfor%}