У меня 4 категорий на моем сайте. Как я могу написать один класс PageViews (ListView) для четырех одинаковых страниц с разными постами. И тот же вопрос, как написать класс DeteilViews (DetailView) для каждого сообщения? Мои модели ниже.
class Category(models.Model):
category = models.CharField(max_length=50)
slug = models.CharField(max_length=60, unique=True)
class Topic(models.Model):
topic = models.CharField(max_length=50)
slug = models.CharField(max_length=60, unique=True)
category = models.ManyToManyField(Category)
class Page(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
datetime = models.DateTimeField()
title = models.CharField(max_length=60)
slug = models.CharField(max_length=250, unique=True)
short_text = models.CharField(max_length=255)
text = models.TextField()
image = models.ImageField(upload_to='media/news_img')
img_source = models.CharField(max_length=255)
page_source = models.CharField(max_length=60)
parsing_date = models.DateTimeField(auto_now=True)
objects = models.Manager()
def get_absolute_url(self):
return reverse('news:detail', args=[self.category.slug, self.topic.slug, self.slug])
class Comment(models.Model):
page = models.ForeignKey(Page, on_delete=models.CASCADE)
comment = models.TextField()
author = models.CharField(max_length=55)
datetime = models.DateTimeField()
Я пытался написать, но не смог ... Например:
class DetailView(DetailView):
template_name = 'post.html'
model = Page
def guery_set(self):
return self.request.GET.get('slug')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = Page.objects.filter(slug=slug)
return context
И:
class PageView(ListView, SingleObjectMixin):
template_name = 'page_body.html'
paginate_by = 8
def get(self, request, *args, **kwargs):
self.object = self.get_object(
queryset=Category.objects.all())
return super().get(request, *args, **kwargs)
def get_queryset(self):
return Page.objects.filter(category=self.object)
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
pages = ????
context['posts'] = pages[:8]
return context