【问题标题】:How to add dropdown list which contains all element in my product list如何添加包含我的产品列表中所有元素的下拉列表
【发布时间】:2019-05-30 04:09:04
【问题描述】:

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

<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

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

【问题讨论】:

  • categories 在哪里定义?向我们展示您的观点。

标签: html css django bootstrap-4


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 2017-08-02
    相关资源
    最近更新 更多