Unable to display data from QuerySet in Django templates
I'm trying to implement a simple search for data on a website. If we specify the a tag in the "search_results.html" template, then all the information that is in it is not displayed, but if we remove the a tag, then everything works, why can this happen?
That is, this information is not displayed for me - <a href="{{ article.get_absolute_url }}">{{ article.title }}</a>
file urls.py
urlpatterns = [
path("search_page/", views.SearchPageView.as_view(), name="searchpageurl")
]
file views.py
class SearchPageView(TemplateView):
template_name = "search/search_results.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
query = self.request.GET.get("q", "")
context["query"] = query
if query:
articles = ListArticles.objects.filter(
Q(title__icontains=query) | Q(text__icontains=query), is_publish=True
)
context["results"] = {"articles": articles}
else:
context["results"] = {}
return context
file model
class ListArticles(models.Model):
title = models.CharField(max_length=100, verbose_name="Заголовок")
text = models.TextField(verbose_name="Текст")
images = models.ImageField(upload_to="articles/", verbose_name="Изображение")
date_publsh = models.DateTimeField(verbose_name="Дата Публикации")
date_modified = models.DateTimeField(auto_now=True)
is_publish = models.BooleanField(verbose_name="Опубликованно/Неопубликованно")
cats = models.ForeignKey(
Category, on_delete=models.CASCADE, verbose_name="Категория"
)
slug = models.SlugField(max_length=100, verbose_name="Слаг")
tag = models.ManyToManyField(Tag, related_name="articles", verbose_name="Теги")
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("singlearticles", kwargs={"detail_slug": self.slug})
file search_results.html
{% extends 'base.html' %}
{% block content %}
<h1>Результаты поиска для "{{ query }}"</h1>
{% if results.articles %}
<h2>Статьи:</h2>
<ul>
{% for article in results.articles %}
<li><a href="{{ article.get_absolute_url }}">{{ article.title }}</a></li>
{% empty %}
<li>Статьи не найдены.</li>
{% endfor %}
</ul>
{% else %}
<p>Ничего не найдено.</p>
{% endif %}
{% endblock %}
I tried to completely remove get_absolute_url from the template and model, as a result, the data is not displayed. The only thing that helps is to remove either the a tag or remove the href parameter from the a tag, after which the data is displayed