L
L
leirons2021-04-21 01:47:39
Django
leirons, 2021-04-21 01:47:39

Started learning Django, got a problem, how to fix it?

I decided to do something like 2ch, the whole plan was already lined up in my head, I made the models, but I can’t figure it out with something.
The code will be below, the question is.
I have a model of an object of communication, a topic for discussion, and there will be posts in the topic for discussion, for example -

Health - Sports - Posts, each topic has a slug that transfers to the template, but I can’t display the posts of a specific slug, I don’t understand more precisely how, I need to display only sports posts, but due to the fact that I don’t know how to transfer only specific sports posts there, I display all the posts in each topic.

Model

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse


class Subject(models.Model):
    title = models.CharField(max_length=50,verbose_name="Название ",)


    class Meta:
        ordering = ['title']
        verbose_name_plural = 'Обьекы обсуждения'
        verbose_name = 'Обьект обсуждения'

    def __str__(self):
        return self.title

class Theme(models.Model):
    title = models.CharField(max_length=50,verbose_name="Название")
    subject = models.ForeignKey(Subject,on_delete=models.CASCADE,related_name='subject')
    slug = models.SlugField(null=True,verbose_name='ссылка на страницу')

    class Meta:
        ordering = ['title']
        verbose_name_plural = 'Темы'
        verbose_name = 'Тема'

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('article_detail', kwargs={'slug': self.slug})

class Post(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE,null=True,blank=True)
    title = models.CharField(max_length=50,verbose_name="Название")
    text = models.TextField(max_length=500,verbose_name="Текст")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='Дата создания')
    is_published = models.BooleanField(default=True)
    where_we_are = models.ForeignKey(Theme,on_delete=models.CASCADE,related_name='post',null=True,blank=True)


    class Meta:
        ordering = ['created_at']
        verbose_name_plural = 'Посты'
        verbose_name = 'Пост'

    def __str__(self):
        return self.title


class Comments(models.Model):
    post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='posts')
    name = models.CharField(max_length=50)
    email = models.EmailField()
    body = models.TextField(max_length=500)
    created_on = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=False)
    every = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments',null=True,blank=True)

    class Meta:
        ordering = ['created_on']
        verbose_name = 'Комментарий'
        verbose_name_plural = 'Комментарии'


View
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic import TemplateView
from django.shortcuts import get_object_or_404
from django.views.generic.edit import DeleteView
from .models import *


class base(ListView):
    model = Theme
    context_object_name = 'themes'
    template_name = 'site/base.html'


class MultipleModelView(TemplateView):
    template_name = 'site/provekra.html'

    def get_context_data(self, **kwargs):
         context = super(MultipleModelView, self).get_context_data(**kwargs)
         context['modeltwo'] = Theme.objects.all()
         context['modelone'] = Subject.objects.all()
         return context

class Themes(MultipleModelView):
    template_name = 'site/themes.html'

    def get_context_data(self, **kwargs):
        context = super(MultipleModelView, self).get_context_data(**kwargs)
        context['modeltwo'] = Theme.objects.all()
        context['modelone'] = Subject.objects.all()
        context['modelthree'] = Post.objects.all()
        return context

    def get_queryset(self):
        return self.Theme.objects.filter(productclass__slug=self.kwargs.get('slug'))


routes
from django.urls import path
from .views import base,MultipleModelView,Themes
urlpatterns = [
    path('',base.as_view(),name='themes'),
    path('', MultipleModelView.as_view(), name='themes'),
    path('<slug:slug>', Themes.as_view(), name='article_detail'),

]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2021-04-21
@deliro

https://docs.djangoproject.com/en/3.2/topics/db/qu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question