gpt4 book ai didi

html - 如何添加包含我的产品列表中所有元素的下拉列表

转载 作者:行者123 更新时间:2023-11-27 23:54:09 24 4
gpt4 key购买 nike

我可以使用迭代不同的产品

<li
{ % if not category % }class = "selected" {% endif % }>

<a href="{% url "shop:product_list" %}">All</a>

</li>

{% for c in categories %}

<li {% if category.slug == c.slug %}class="selected"{% endif %}>
<a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
</li>
{% endfor %}

这是我的下拉列表代码,但在书籍之后没有显示任何内容。

<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
Categories
</a>
<p class=" dropdown-menu ">
<a class="dropdown-item" href="{% url 'shop:product_list' %}">All</a>
<a class="dropdown-item" href="#">Electronics</a>
<a class="dropdown-item" href="#">Books</a>

<ul>
<li {% if not category %}class="selected"{% endif %}>
<a href="{% url "shop:product_list" %}">All</a>
</li>
{% for c in categories %}
<li {% if category.slug == c.slug %}class="selected"{% endif %}>
<a class="dropdown-item" href="{{ c.get_absolute_url }}">{{ c.name }}</a>
</li>
{% endfor %}
</ul>

</p>
</li>


我想将这段代码添加到我的下拉列表中

        <li {% if not category %}class="selected"{% endif %}>
<a href="{% url "shop:product_list" %}">All</a>
</li>
{% for c in categories %}
<li {% if category.slug == c.slug %}class="selected"{% endif %}>
<a class="dropdown-item" href="{{ c.get_absolute_url }}">{{ c.name }}</a>
</li>
{% endfor %}

但是这段代码在下拉列表中没有显示任何内容

我的 views.py 文件:

from django.shortcuts import render, get_object_or_404

from .models import Category, Product


def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)

return render(request, 'shop/products/list.html', {
'category': category,
'categories': categories,
'products': products
})

我的模型.py:

from django.db import models
from django.urls import reverse


class Category(models.Model):
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True)

class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'

def __str__(self):
return self.name

def get_absolute_url(self):
return reverse('shop:product_list_by_category', args=[self.slug])


class Product(models.Model):
category = models.ForeignKey(Category, related_name='products', on_delete=True)
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True)
image = models.ImageField(upload_to='products/%y/%m/%d', blank=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now_add=True)

class Meta:
ordering = ('-created',)
index_together = (('id', 'slug'),)

def __str__(self):
return self.name

-------------------------------------------- ----------------------------------

最佳答案

我认为这是因为您将 ul 嵌套在 p 中,它是无效的 HTML。

关于html - 如何添加包含我的产品列表中所有元素的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56365033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com