简体   繁体   中英

Category based list View in django

I have two model 1 is category, 2nd is details and I want make a view where i can click on a category and the link should take me to the list only for the current category what i clicked. my models are given:

class FoodCategory(models.Model):
    categoryname = models.CharField(max_length=100)
    categorydetails = models.CharField(max_length=1000)
    categoryimage = models.ImageField(default='cat_def.jpg', 
    upload_to='catimg')

    class Meta:
        verbose_name = 'Food Category'

    def __str__(self):
        return self.categoryname


class FoodDetails(models.Model):
    fromcategory = models.ForeignKey(FoodCategory, 
    on_delete=models.CASCADE)
    foodname = models.CharField(max_length=100)
    fooddetails = models.CharField(max_length=1000)
    foodimage = models.ImageField(default='food_def.jpg', 
    upload_to='fodimg')
    armodel = models.CharField(default='andy.sfb', max_length=50)
    additiondate = models.DateTimeField(default=timezone.now)
    addedby = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Food Detail'

    def __str__(self):
        return self.fromcategory.categoryname + \
           ' - ' + self.foodname + \
           ' - ' + 'added by' + \
           ' - ' + self.addedby.username

    def get_absolute_url(self):
        return reverse('food-details', kwargs={'pk': self.pk})

views.py file:

from django.shortcuts import render
from food.models import FoodCategory, FoodDetails
from django.views.generic import ListView, DetailView


class FoodCategoryView(ListView):
    model = FoodCategory
    template_name = 'food/food-category.html'
    context_object_name = 'food'


class FoodListView(ListView):
    model = FoodDetails
    template_name = 'food/food-list.html'
    context_object_name = 'food'

    ordering = ['-additiondate']


class FoodDetailsView(DetailView):
    model = FoodDetails
    template_name = 'food/FoodDetails_detail.html'

and my urls.py file also:

from django.urls import path, include
from . import views
from .views import FoodCategoryView, FoodListView, FoodDetailsView

# api url handler

urlpatterns = [
    path('category/', FoodCategoryView.as_view(), name='food- 
    category'),
    path('foodlist/', FoodListView.as_view(), name='food-list'),
    path('foodlist/<int:pk>/', FoodDetailsView.as_view(), 
    name='food-details')

]

here is my all files and i just want know a specific way to get the current category based food details.

You can do this by defining get_queryset on a listview.

class FoodDetailsByCategory(ListView):
    def get_queryset(self):
        return FoodDetails.objects.filter(fromcategory_id=self.kwargs['category_id'])

with a simple URL:

path('category/<int:category_id>', FoodDetailsByCategory.as_view(), name='food_details_by_category')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM