Reputation: 1
Am creating a blog and a news website in django which have a specific category displayed on its hompage. That is the specific category for news, Politics and education are beeing displayed on my post_list.html. I get a no exception provided when i try to retrieve it by
Post.objects.filter(category__name__in=["news"])
This is the model for my category
class Category(models.Model):
name = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique=True)
This is the model for my Post
class Post(models.Model):
image = models.ImageField(upload_to="images/")
title = models.CharField(max_length=150)
summary = models.CharField(max_length=250)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
*This is the view for my postlist
class PostListView(ListView):
model = Post
template_name = blog/post_list.html
context_object_name = "posts"
ordering = ["date_posted"
paginate_by = 5
def get_queryset(self):
posts =
get_object_or_404(Post)
return Post.objects.filter().order_by( "-date_posted")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = Post.objects.filter()[0:4]
context['new_cat'] =Post.objects.filter(category__name__in=[" news"])[0.5]
context['new_politics'] = Post.objects.filter(category__name__in=["politics"])[0.5]
return context ***
This is my pos_tlist.html code for retrieving post by category in news
<div> <h1>News</h1> <div/>
{% for post in new_cat%}
div class="mt-2"
<a href="{{ post.get_absolute_url }}"><img class="object-cover w-full rounded-lg" src="{{ post.image.url }}" alt="{{ post.title }} cover"> </a></div>
<div class="py-2 flex flex-row items-center justify-between">
<a href="{{ post.get_absolute_url }}"><h1 class="hover:text-blue-600 text-gray-900 font-bold text-xl mb-2 py-2">{{ post.title }}</h1></a></div> '''
I get a no exception provided for this post when i run python manage.py runserver But when i go to my shell , python manage.py shell I get a full list of all the post. But it refuses to display on hompage I need help in doing this and making all my category appear per category on news page Is there any solution for all my post to appear category by category on post_list.html and homepage
In summary- i can't filter post that have a Foreign key Models like Category, Author in my Post Model. I can directly filter post from the post model but i can't filter post title from the category Name models
That means when i try to filter Post.objects.all() then all my post will come out but when i try Post.objects.filter(category__name__in=["news"]) nothing will show in my page except no exeption error
Help
Upvotes: 0
Views: 186
Reputation: 10227
Could a problem be in whitespace you have in " news" here?
Post.objects.filter(category__name__in=[" news"])[0.5]
Also, what is [0.5]
? I guess you want [0:5]
there?
Also, make sure your get_queryset
and get_context_data
methods are indented properly. Because if they have no indent as you showed it then they defined outside of your PostListView
class...
Upvotes: 2