将 context_processor 数据返回到 html 进行循环时出现问题

问题描述 投票:0回答:1

我正在尝试将类别字段添加到导航中。到目前为止,我已经制作了 context_processor 来查询所有汽车品牌。在我的导航中,我循环了所有品牌,这很有效。我还想添加包含某些汽车品牌的某些汽车型号的下拉菜单。我认为问题在于返回正确的数据,这在我的 _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 %}

context_processors.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
        }

如果打印,它会给我不同的列表,由不同的模型组成,例如。 [] [] []

型号:

产品类别型号:

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。我希望我的 naviagion 菜单中的每个汽车品牌都有下拉菜单,其中包含某些汽车型号。

python django list dictionary return
1个回答
0
投票

你把事情搞得太复杂了,你可以使用:

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>
© www.soinside.com 2019 - 2024. All rights reserved.