I am trying to do a search over many data models in Django to my site. Now I have code like this in views.
class Search(ListView):
template_name="search.html"
context_object_name="result_serch_all"
paginate_by = 10
def get_queryset(self):
news = Articles.objects.filter(title__icontains=self.request.GET.get("q"))
docs = Doc.objects.filter(name_doc__icontains=self.request.GET.get("q"))
query_sets = list(chain(news, docs))
return query_sets
def get_context_data(self, *args, **kwargs):
paginator = Paginator(self.get_queryset(), 5)
context = super().get_context_data(*args, **kwargs)
context["q"] =f'q={self.request.GET.get("q")}&'
print(context['result_serch_all'])
return context
In the template, I output like this
{%for el in result_serch_all%}
<div> {{el}} </div>
{% endfor %}
How can the output in the template be divided into blocks?So that in front of all found articles there is an inscription Found in the news section. Before the documents Found in the documents section`?
I found this option. In my model im add
def get_my_model_name(self):
return self._meta.model_name
and in the template I check
{% if el.get_my_model_name == 'articles' %}
Maybe there is some standard way in Django?