Hello,
I’m newbee to Django and sitting in stuck. I need to send multiple model filter to frontend and how to do that ?
Class based view
code:
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .models import Post
# Create your views here.
class BlogListView(ListView):
queryset = Post.objects.filter(status=1).order_by("-created_on")
template_name = "home.html"
I need also to send Post.objects.filter(is_top=True) and how is it possible to send multiple data to frontend ?
You need to be more specific than this for us to be able to help you.
What do you mean by “multiple model filter”?
What “frontend” are you using?
Have you worked your way through the Official Django Tutorial? If so, then review the work you’ve done on the ListView
in the polls app.
Hi Kenn,
I found out by my self. It works but I think it’s correct way to do that. What I means is this hope you can figure out what I mean by looking my code.
I’m using class based view. I think it’s called like that.
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .models import Post
# Create your views here.
class BlogListView(ListView):
queryset = Post.objects.filter(status=1).order_by("-created_on")
template_name = "home.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# context["post_list"] = Post.objects.filter(status=1).order_by("-created_on")
context["top"] = Post.objects.filter(is_top=True).first()
context["featured_posts"] = Post.objects.filter(is_featured=True).all()
return context