【发布时间】:2017-07-07 02:04:01
【问题描述】:
我的类别很少。说电子产品和玩具。我在商场里有多家商店。商店使用外键(类别)保存。现在在导航栏中.. 我想根据他们的类别列出商店。感谢期待
models.py
class ShopCategories(models.Model):
category = models.CharField(max_length=50, unique=True,)
def __str__(self):
return self.category
class NewShop(models.Model):
category = models.ForeignKey(ShopCategories)
name = models.CharField(max_length=100, unique=True)
tagline = models.CharField(max_length=50, default='Enter tagline here2')
description = models.TextField(default='enter shop description')
def __str__(self):
return self.name
views.py
def basefile(request):
shop_cat = NewShop.objects.filter(category_id=1)
shop_name = NewShop.objects.filter(name=shop_cat)
return render_to_response('base.html', {'Shopname':shop_name, 'Shopcat':shop_cat})
base.html
{% for category_id in Shopcat %}
<li><a href="#">{{ Shopname }}</a></l>
{% endfor %}
【问题讨论】:
-
我们需要您详细说明您的需求。我的意思是,您是否需要一个根据类别选择显示商店列表的下拉菜单?
-
@BrianOcampo .. 没错。取决于类别选择和类别下的商店列表的下拉列表
标签: django python-2.7 django-models django-templates