Function mixing up values of different model (within similar named fields)

Viewed 25

Here is my search function

def search(request):
    query = request.GET['query']
    allPoststit = List.objects.filter(title__icontains=query)
    allPostscont = List.objects.filter(content__icontains=query)
    allPostsl = allPoststit.union(allPostscont)
    allPoststitm = MusicList.objects.filter(title__icontains=query)
    allPostscontm = MusicList.objects.filter(content__icontains=query)
    allPostsm = allPoststitm.union(allPostscontm)
    allPoststitb = BookList.objects.filter(title__icontains=query)
    allPostscontb = BookList.objects.filter(content__icontains=query)
    allPostsb = allPoststitb.union(allPostscontb)
    allPosts2 = allPostsl.union(allPostsm)
    allPosts = allPosts2.union(allPostsb)
    params = {'allPosts' : allPosts, 'query':query}
    return render(request, 'app/search.html', params)

when from template im calling

{% for tag in i.genre.all %}
<div class="Genre">
<small>{{ tag }}</small>
</div>
{% empty %}
            No tags!
{% endfor %}

Its mixing up values within different models or returning empty. Any idea why is this happening?

1 Answers

First, not allBlabla, it is all_blablabla. And you don't even send some "genre" or "i" object into your context, you send "allPosts" and "query", so why do you try to loop through something that does not exist in your context, in your template? Because you have absolutely no clue about what you are doing. So that is a bit harsh, but it is what it is.

Related