Error sending queryset data to template

Hi everyone
I am designing a compare product section in my web store app
in that page there is a modal that when you want to add another product for comparing it displays all related product that have similar branch as first product
in that modal there is a search box that you could look for a product and if the search is successful it displays that product within the modal (to add to compare page)
the problem is i retrieve searched product from DB (by using good_to_compare function) but when i want to display the result to template(using get_queryset) i get this error
" ‘dict’ object has no attribute 'headers '"

here is

class GoodCompareView(DetailView):
    model = Good
    template_name = 'good_compare.html'

    def get_queryset(self):
        pk = self.kwargs.get(self.pk_url_kwarg)
        searched_good = compare_search_modal(self.request, pk)
        if searched_good is not None:
            result = compare_search_modal(self.request, pk)
            query = result.get('next_good')            
            related_objects = Good.objects.filter(id=pk).values(
                'mainbranch', 'subbranch1', 'subbranch2')
            query = Good.objects.filter(mainbranch=related_objects[0]['mainbranch'],
        return query

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['compare_form'] = GoodCompareForm
        context['related_goods'] = self.get_queryset()
        return context

def compare_search_modal(request, pk):
    form = GoodCompareForm(request.GET)
    if request.method == 'GET':
        if form.is_valid():
            search_phrase = form.cleaned_data['search_box']
            name_query = SubBranch3.objects.filter(sb3_desc__icontains=search_phrase).values('id')
            good_to_compare = Good.objects.filter(subbranch3_id__in=name_query)
            if good_to_compare.exists() and name_query.exists():
                next_pk = Good.objects.filter(subbranch3_id__in=name_query).values('id')[0]['id']
                context = {
                    'next_good': good_to_compare,
                    'next_pk': next_pk
                return context
                messages.error(request, 'کالایی با این مشخصات یافت نشد')
                return HttpResponseRedirect(reverse('good_compare', kwargs={'pk': pk}))
        return HttpResponseRedirect(reverse('good_compare', kwargs={'pk': pk}))

and product_compare template

{% extends '_base.html' %}
{% load static %}
{% load crispy_forms_tags %}

{% block title %}مقایسه کالا{% endblock title %}

{% block content %}
{% if messages %}
    {% for message in messages %}
        {% if message.tags == "success" %}
            <div class="alert alert-success d-flex align-items-center" role="alert">
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    {{ message }}
        {% else %}
            {% if message.tags == "error" %}
                <div class="alert alert-danger d-flex align-items-center" role="alert">
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        {{ message }}
            {% endif %}
        {% endif %}
    {% endfor %}
{% endif %}

<h4 align="center"> مقایسه کالا </h4>
           <div class="container text-right">
               <div class="row">
                   <div class="col-md-auto">
                       <div class="">
                           <div class="card" style="width: 18rem;">
                           <img src="{% static object.image %}" class="card-img-top"
                                alt="عکس محصول" height="250" width="200">
                           <div class="card-body">
                               <h5 class="card-title">{{object.subbranch3.sb3_desc}}</h5>
                               <p class="card-text">قیمت:{{object.price}}ریال</p>
                               <p class="card-text"></p>
                               <p> {{next_pk}} </p>
                               <a href="{% url 'goods_list' %}"
                                    class="btn btn-primary stretched-link">مشاهده جزئیات</a>
               <div class="card" style="width: 18rem;">
                   {% for att in object.attributes.all %}
                       <div class="card card-body">
                           <div class="row row-col-2">
                               <div class="col">
                               <div class="col">
                   {% endfor %}
           <div class="row">
               <div class="col-md-auto">
                    <!-- Button trigger modal -->
                   <button type="button" class="btn btn-primary" data-bs-toggle="modal"
                    افزودن کالا
               <!-- Modal -->
                   <div class="modal fade" id="searchModal" tabindex="-1" data-bs-backdrop="static"
                     aria-labelledby="searchModalLabel" aria-hidden="true">
                   <div class="modal-dialog">
                       <div class="modal-content">
                           <div class="modal-header">
                               <h1 class="modal-title fs-5" id="searchModalLabel">جستجوی کالا</h1>
                               <button type="button" class="btn-close" data-bs-dismiss="modal"
                           <div class="modal-body">
                               <form action="{% url 'compare_search' %}" method="get">{% csrf_token %}
                                   <button class="btn btn-primary" type="submit">جستجو</button>
                               <div class="row"><!-- section for displaying related goods
                                                   base on present good subbranch -->
                                   {% for good in related_goods %}
                                   <div class="col-md-auto">
                                       <div class="card" style="width: 13rem; height: 13rem">
                                           <img src="{% static good.image %}" class="card-img-top"
                                                alt="عکس محصول" height="100" width="50">
                                           <div class="card-body">
                                               <p class="card-title">{{good.subbranch3.sb3_desc}}</p>
                                               <p class="card-text">قیمت:{{good.price}}ریال</p>
                                               <p class="card-text"></p>
                                               <a href="{% url 'good_compare' %}"
                                                  class="btn btn-primary stretched-link btn-sm">مشاهده جزئیات</a>
                                   {% endfor %}
                           <div class="modal-footer">
                               <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">بستن</button>

{% endblock content %}

and urls .py

urlpatterns = [
    path('about/', AboutPageView.as_view(), name='about'),
    path('?/', GoodsSearchView.as_view(), name='gsview'),
    path('?/<int:pk>/', GoodsListView.as_view(), name='goods_list'),
    path('?/<int:g_id>/wish_list/<int:c_id>/', add_to_wishlist, name='add_to_wishlist'),
    path('?/<int:g_id>/buy_basket/', add_to_buybasket, name='add_to_buybasket'),
    path('?/<str:from_link>&&<int:search_phrase>', GoodsSearchView.as_view(), name='branch_filter'),
    path('?/<int:pk>/edit_comment/<int:cpk>/', CommentUpdate.as_view(), name='edit_comment'),
    path('?/<int:pk>/delete_comment/<int:cpk>/', CommentDelete.as_view(), name='delete_comment'),
    path('?/<int:pk>/vote_comment/<int:cpk>', VoteComment.as_view(), name='comment_votes'),
    path('?/<int:pk>/good_compare', GoodCompareView.as_view(), name='good_compare'),
    path('?/<int:pk>/good_compare/search/', compare_search_modal, name='compare_search'),

When you are requesting assistance with an error, please post the text of the complete error with the traceback.

Here is traceback


Request Method: GET
Request URL:

Django Version: 5.0.1
Python Version: 3.10.13
Installed Applications:
Installed Middleware:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/", line 55, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.10/site-packages/django/utils/", line 136, in __call__
    response = self.process_response(request, response)
  File "/usr/local/lib/python3.10/site-packages/django/middleware/", line 34, in process_response
    response.headers["X-Frame-Options"] = self.get_xframe_options_value(

Exception Type: AttributeError at /goods/?/5/good_compare/search/
Exception Value: 'dict' object has no attribute 'headers'

This function is a view.

Because it is a view, it receives a request and must return an HttpResponse.

You cannot directly return a dict from it.

The problem is here:

Thanks for your answer
what i want to do is :
return a queryset based on search phrase(in modal)
set queryset based on returned result of step before
so how could i do that?

Return the queryset, to where?

If to the browser, then what are you going to do with it there?

Remember, template rendering occurs in the server, not the browser. If you return a queryset to the browser, then it’s up to you to write the JavaScript to convert that queryset to HTML to be shown on the page.

(Also, in the case where you’re trying to return data to the browser, you almost always want to do that in an AJAX call, not in a GET request.)

You also need to keep in mind that a browser “modal” is still part of the same page as everything else on that page. HTML does not support the concept of “multiple pages in the same window”, so libraries like bootstrap “fake out” the user by displaying a div as if it were a different window. But, you’re still looking at the same page - just a different part of it.
(Why is this important? Because it can help you simplify your code while you’re trying to work on both sides of this exchange. Your Django code can be the same regardless of whether you’re trying to render something in a modal or as part of the main page. This means you could remove the modal from your template and allow Django to render what it needs to render on the page. Get the Django code working that you need to have working, then convert this back into using a modal.)

Thanks for you answer
now i am going to learn some AJAX tricks to do what i want to do