我正在try 将类别字段添加到导航.到目前为止,我已经制作了上下文处理器,它可以查询所有的汽车品牌.在我的导航中,我在所有品牌中循环,这很管用.我还想添加下拉菜单,其中包含某些汽车品牌的某些车型.我认为问题在于返回正确的数据,这在我的_base.html中是可以循环的.

_base.html


<div class="flex pt-3 container w-full">

  {% for item in car_brands %}

    <button id="dropdown-button" data-dropdown-toggle="dropdown-{{item.id}}" class="py-2.5 px-4 text-sm font-medium text-center text-gray-900 bg-gray-100 uppercase" type="button">{{item}}</button>
    <div id="dropdown-{{item.id}}" class="hidden shadow w-44 bg-gray-100">
      <ul aria-labelledby="dropdown-button">
        {% for model in models %}
        <li>
          <a href="#" class="inline-flex w-full px-4 py-2 hover:bg-gray-50">{{ model }}</a>
        </li>
        {% endfor %}
      </ul>
    </div>

  {% endfor %}

上下文_Procesors.py

from .models import CarBrandCategory, CarModel

def brand_catalogue(request):
    car_brands = CarBrandCategory.objects.all()


    for brand in car_brands:
        models = [model for model in CarModel.objects.filter(brand_id=brand.id)]
        print(models)


    return {
        'car_brands': car_brands,
        'models': models
        }

如果打印,它会给我不同的 list ,由不同的型号组成,例如.

[<CarModel: X4>, <CarModel: X5>, <CarModel: 3. Seeria>]
[<CarModel: A4>, <CarModel: A6>, <CarModel: A8>]
[<CarModel: C klass>, <CarModel: S-Klass>]

型号:

产品类别型号:

from django.db import models

# Main product category

class ProductCategory(models.Model):
    category_name = models.CharField(max_length=255)
    category_description = models.TextField()

    def __str__(self):
        return self.category_name
    
# Condition category

class ProductCondition(models.Model):
    condition = models.CharField(max_length=100)

    def __str__(self):
        return self.condition
    
# Band Category

class CarBrandCategory(models.Model):
    brand_name = models.CharField(max_length=30, blank=True)

    def __str__(self):
        return self.brand_name
    
# Brand Model Category
    
class CarModel(models.Model):
    model = models.CharField(max_length=100, blank=True)
    brand = models.ForeignKey(CarBrandCategory, on_delete=models.CASCADE, related_name='model')

    def __str__(self):
        return self.model

产品型号

class Product(models.Model):
    product_title = models.CharField(max_length=255)
    product_description = models.TextField()
    product_description_short = models.CharField(max_length=255, blank=True)
    product_condition = models.ForeignKey(ProductCondition, on_delete=models.CASCADE)
    product_code = models.CharField(max_length=100, blank=True)
    product_manufacturer = models.CharField(max_length=100, blank=True)
    product_location_state = models.CharField(max_length=100)
    product_location_city = models.CharField(max_length=100, blank=True)
    product_delivery_time = models.IntegerField(null=True, blank=True)
    product_category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE, related_name='products')
    product_brand = models.ForeignKey(CarBrandCategory, on_delete=models.CASCADE, related_name='brand')
    product_model = models.ForeignKey(CarModel, on_delete=models.CASCADE)
    product_img_1 = models.ImageField(upload_to=new_image_path, blank=True)
    product_img_2 = models.ImageField(upload_to=new_image_path, blank=True)
    product_img_3 = models.ImageField(upload_to=new_image_path, blank=True)
    product_img_4 = models.ImageField(upload_to=new_image_path, blank=True)
    product_img_5 = models.ImageField(upload_to=new_image_path, blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, default=0.0)
    product_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user')
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.product_title
    

我想知道是否有人对这种解决方案有经验,创建这种解决方案的最佳方式是什么?

我已经创建了一个不同车型的列表,但我无法将其返回到HTML.我希望我的导航菜单中的每个汽车品牌都有下拉菜单,其中包括某些车型.

推荐答案

你把事情搞得过于复杂了,你可以处理:

from .models import CarBrandCategory


def brand_catalogue(request):
    return {'car_brands': CarBrandCategory.objects.prefetch_related('model')}

然后,在模板中使用:

<ul>
    {% for brand in car_brands %}
        <li>{{ brand.brand_name }}
        {% for model in brand.model.all %}
            <li>{{ model.model }}</li>
        {% endfor %}
        </li>
    {% endfor %}
</ul>

Python相关问答推荐

提取两行之间的标题的常规表达

Polars比较了两个预设-有没有方法在第一次不匹配时立即失败

将jit与numpy linSpace函数一起使用时出错

_repr_html_实现自定义__getattr_时未显示

如何从在虚拟Python环境中运行的脚本中运行需要宿主Python环境的Shell脚本?

Stacked bar chart from billrame

Odoo 16使用NTFS使字段只读

Plotly Dash Creating Interactive Graph下拉列表

在Python 3中,如何让客户端打开一个套接字到服务器,发送一行JSON编码的数据,读回一行JSON编码的数据,然后继续?

为什么numpy. vectorize调用vectorized函数的次数比vector中的元素要多?

pandas:对多级列框架的列进行排序/重新排序

重置PD帧中的值

ConversationalRetrivalChain引发键错误

Odoo16:模板中使用的docs变量在哪里定义?

将一个双框爆炸到另一个双框的范围内

无法在Spyder上的Pandas中将本地CSV转换为数据帧

根据Pandas中带条件的两个列的值创建新列

Pandas:计数器的滚动和,复位

一维不匹配两个数组上的广义ufunc

try 在单个WITH_COLUMNS_SEQ操作中链接表达式时,使用Polars数据帧时出现ComputeError