URL喜欢2种型号CBV的树



我有3个模型:

  1. DISH(FK DISCLASS)
  2. distype(fk distype)
  3. 唱片

唱片可以没有洗碗机。示例:

1)obj1 dishtype = Margarita; dishclass = Pizza
2)obj2 dishtype = Alfonso; dishclass = Pizza
3)obj3 dishtype = Burger; dishclass = none

然后,我在url/dish/:披萨(如果OBJ有洗碗机,只显示唱片频道)汉堡

我想要这个逻辑:

如果我单击Pizza-> /dish/pizza/并具有对象MargaritaAlfonso-> clik to Pizza的名称,然后转到/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')

您应该编写一个通用视图而不是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

最新更新