我有3种型号:
Dishclass可以没有菜型。 示例:
1)obj1 dishtype = Margarita; dishclass = Pizza
2)obj2 dishtype = Alfonso; dishclass = Pizza
3)obj3 dishtype = Burger; dishclass = none
然后,我用URL / dish /上的对象制作ListView: 披萨(如果obj具有盘类,则仅显示盘类) 汉堡
我想要这个逻辑:
如果我单击到Pizza
-> /dish/pizza/
,并且有对象Margarita
和Alfonso
->轻敲比萨饼的名称,然后转到/dish/pizza/margarita
等。
如果我单击到Burger
-> /dish/burger/
现在我的代码只能这样工作:/dish/burger/
和/dishclass/pizza
我尝试以下代码:
模型
class DishClass(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField()
def get_absolute_url(self):
return reverse('dishclass_detail', kwargs={'slug': self.slug})
class DishType(models.Model):
name = models.CharField(max_length=50, blank=True, null=True, default=None)
dishclass = models.ForeignKey(DishClass, blank=True, null=True)
slug = models.SlugField()
def get_absolute_url(self):
return reverse('dish_detail', kwargs={'slug': self.slug})
class Dish(models.Model):
dishtype = models.ForeignKey(DishType)
name = models.CharField(max_length=100)
slug = models.SlugField()
观看次数
class DishListView(ListView):
model = Dish
template_name = 'dish.html'
def get_context_data(self, *args, **kwargs):
context = super(DishListView, self).get_context_data(*args, **kwargs)
alltypes = DishType.objects.all()
all_dish = Dish.objects.all()
dict = {}
k = 0
for category in alltypes:
for dishes in all_dish:
if dishes.dish.dishtype.name == category.name:
k=k+1
if category.dishclass:
if category.dishclass in dict:
dict[category.dishclass] += k
else:
dict[category.dishclass] = k
k = 0
else:
dict[category] = k
k = 0
context['dict'] = sorted(dict.items(), key=operator.itemgetter(1), reverse=True)
return context
class DishTypeView(TemplateView):
template_name = 'dish_type.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['dish_obj_by_type'] = Dish.objects.filter(dishtype__slug=DishType.objects.get(slug=kwargs['dish_type_slug']).slug)
return context
class DishClassView(TemplateView):
template_name = 'dish_class.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# context['if_dishclass_is_none'] = Dish.objects.filter(dishtype__slug=DishType.objects.get(slug=kwargs['dish_class_slug']).slug)
context['dish_type'] = DishType.objects.filter(dishclass__slug=DishClass.objects.get(slug=kwargs['dish_class_slug']).slug)
return context
URLS
url(r'^dish/$', DishListView.as_view(), name='dish'),
url(r'^dish/(?P<dish_class_slug>[\w-]+)/(?P<dish_type_slug>[\w-]+)/$', DishTypeView.as_view(), name='dish_type'),
url(r'^dish/(?P<dish_class_slug>[\w-]+)/$', DishClassView.as_view(), name='dish_class')
答案 0 :(得分:0)
您应该编写通用视图而不是DetailView
,然后根据提供的kwargs
将所需的对象收集在一起;
url(
r'^dish/(?P<dish_class_slug>[\w-]+)/(?P<dish_type_slug>[\w-]+)/$',
DishTypeView.as_view(), name='dish_type'
),
url(
r'^dish/(?P<dish_class_slug>[\w-]+)/$',
DishClassView.as_view(), name='dish_class'
),
查看示例;
from django.http import HttpResponse
from django.views import TemplateView
from .models import DishClass, DishType
class DishTypeView(TemplateView):
template_name = 'dish_type.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['dish_class'] = DishClass.objects.get(slug=kwargs['dish_class_slug'])
context['dish_type'] = DishType.objects.get(slug=kwargs['dish_type_slug'])
return context