我正在使用django 2.0.5 shop建立一个电子商务网站,我被困在为我的产品实施子类别,我无法在Google和S.O上得到答案
我的代码:
models.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,unique=True)
#parent_category = models.ForeignKey('self', null=True, blank=True,on_delete=models.CASCADE)
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=models.CASCADE)
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=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=True)
class Meta:
ordering = ('name',)
index_together = (('id', 'slug'),)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_detail',
args=[self.id, self.slug])
admin.py
from django.contrib import admin
from .models import Category, Product
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ['name', 'slug']
prepopulated_fields = {'slug': ('name',)}
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
list_display = ['name', 'slug', 'price', 'stock',
'available', 'created', 'updated']
list_filter = ['available', 'created', 'updated']
list_editable = ['price', 'stock', 'available']
prepopulated_fields = {'slug': ('name',)}
views.py
from django.shortcuts import render, get_object_or_404
from .models import Category, Product
from cart.forms import CartAddProductForm
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
# subcategories = Category.objects.filter(parent_category__id=target_category.id)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request, 'shop/product/list.html', {
'category': category,
'categories': categories,
'products': products
})
def product_detail(request, id, slug):
product = get_object_or_404(
Product,
id=id,
slug=slug,
available=True
)
cart_product_form = CartAddProductForm()
return render(request, 'shop/product/detail.html', {
'product': product,
'cart_product_form': cart_product_form
})
urls.py
from django.urls import path
from . import views
app_name = 'shop'
urlpatterns = [
path('', views.product_list, name='product_list'),
path('<slug:category_slug>/', views.product_list,
name='product_list_by_category'),
path('<int:id>/<slug:slug>/', views.product_detail, name='product_detail'),
]
用于查看的html模板:
<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 href="{{ c.get_absolute_url }}">{{ c.name }}</a>
</li>
{% endfor %}
</ul>
类别和子类别应在管理界面中键入 我希望它在每个类别下生成一个下拉嵌套列表
例如电子产品,电话,平板电脑