Skip to content
Advertisement

How to show all details of the stored post while also hiding the query set

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:

<QuerySet [<Post: sports | Kaushik>]> 

What I want:

**Sports** - **Sports** - **Kaushik Jay** - June 16, 2021 - (**Edit**) (**Delete**)
Click link Above to reach the blog

views.py:

def search_posts(request):
    if request.method == "POST":
        searched = request.POST['searched']
        post = Post.objects.filter(title__icontains=searched)


        return render(request, 'search_posts.html',{'searched':searched, 'post':post})

    else:

        return render(request, 'search_posts.html',{})

search_posts.html:

{% extends 'base.html' %}

{% block content %}
    
    {% if searched %}
        <h1>Search Results For {{ searched }}</h1>
        <br/>
        {% for posts in post %}
            {{ post }}<br/>
        {% endfor %}

    {% else %}
        <h1>Hey!! You Did Not Search For Anything.</h1>

    {% endif %}
    

    <br/><br/>
    <a href="{% url 'home' %}" class="btn btn-secondary">Back</a>
{% endblock %}

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement