While I was adding a search bar in my blog. I had a design error I keep on getting query sets. Also when I search for the name of the post I want it show the name, snippet, author and others.(Example below with code.) Also Everything bold are meant to be a link.
What it shows:
JavaScript
x
2
1
<QuerySet [<Post: sports | Kaushik>]>
2
What I want:
JavaScript
1
3
1
**Sports** - **Sports** - **Kaushik Jay** - June 16, 2021 - (**Edit**) (**Delete**)
2
Click link Above to reach the blog
3
views.py:
JavaScript
1
12
12
1
def search_posts(request):
2
if request.method == "POST":
3
searched = request.POST['searched']
4
post = Post.objects.filter(title__icontains=searched)
5
6
7
return render(request, 'search_posts.html',{'searched':searched, 'post':post})
8
9
else:
10
11
return render(request, 'search_posts.html',{})
12
search_posts.html:
JavaScript
1
21
21
1
{% extends 'base.html' %}
2
3
{% block content %}
4
5
{% if searched %}
6
<h1>Search Results For {{ searched }}</h1>
7
<br/>
8
{% for posts in post %}
9
{{ post }}<br/>
10
{% endfor %}
11
12
{% else %}
13
<h1>Hey!! You Did Not Search For Anything.</h1>
14
15
{% endif %}
16
17
18
<br/><br/>
19
<a href="{% url 'home' %}" class="btn btn-secondary">Back</a>
20
{% endblock %}
21
Advertisement
Answer
You named the list of posts as post
and in your template you iterate as for posts in post
but you display the post
.