首先让我说我是 Django 和一般编程的新手。花时间学习 python 并在一个多月前开始使用 django。我可以尝试在具有下拉菜单的电子商务应用程序上创建浏览点击,您可以在其中选择一个类别并对其进行排序。我认为我需要做的工作是在视图中,但不确定要编写的代码。
Views:
def browse(request):
if request.method == 'POST':
form = CategoryChoicesForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/')
model:
CATEGORY_CHOICES = (
('Accessories', 'Accessories'),
('Bags', 'Bags'),
('Basic Tees', 'Basic Tees'),
('Belts', 'Belts'),
('Blazers', 'Blazers'),
('Boots', 'Boots'),
('Button Downs', 'Button Downs'),
('Cardigans', 'Cardigans'),
('Denim', 'Denim'),
('Footwear', 'Footwear'),
('Gloves', 'Gloves'),
('Hats', 'Hats'),
('Headphones', 'Headphones'),
('Henleys', 'Henleys'),
('Home', 'Home'),
('Jackets', 'Jackets'),
('Jewelry', 'Jewelry'),
('Outerwear', 'Outerwear'),
('Other', 'Other'),
('Pants', 'Pants'),
('Polos', 'Polos'),
('Scarves', 'Scarves'),
('Shirts', 'Shirts'),
('Shoes','Shoes'),
('Sneakers', 'Sneakers'),
('socks', 'Socks'),
('Sunglasses', 'Sunglasses'),
('Sweaters', 'Sweaters'),
('Sweatshirts', 'Sweatshirts'),
('Tank Tops', 'Tank Tops'),
('Tech', 'Tech'),
('T-Shirts', 'T-Shirts'),
('Undergarments', 'Undergarments'),
('Wallets', 'Wallets'),
('Watches', 'Watches'),
)
class Item(models.Model):
item_name = models.CharField(max_length=120)
item_picture = models.ImageField(upload_to="images/")
item_description = models.CharField(max_length=999, blank=True)
size = models.CharField(max_length=8, choices=SIZE_CHOICES, blank=True)
item_brand = models.ForeignKey('Brand')
category = models.CharField(max_length=25, choices=CATEGORY_CHOICES)
def __unicode__(self):
return self.item_name
Form:
class CategoryChoicesForm(forms.Form):
items = Item.get_category_display
categoryoption = forms.ModelChoiceField(queryset=items, empty_label=None)