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 views.py
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')
else:
related_objects = Good.objects.filter(id=pk).values(
'mainbranch', 'subbranch1', 'subbranch2')
query = Good.objects.filter(mainbranch=related_objects[0]['mainbranch'],
subbranch1=related_objects[0]['subbranch1'],
subbranch2=related_objects[0]['subbranch2'])
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
else:
messages.error(request, 'کالایی با این مشخصات یافت نشد')
return HttpResponseRedirect(reverse('good_compare', kwargs={'pk': pk}))
else:
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 }}
</div>
{% 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 }}
</div>
{% 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' pk=object.pk %}"
class="btn btn-primary stretched-link">مشاهده جزئیات</a>
</div>
</div>
</div>
<div>
<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">
<P>{{att.attribute.attribute_name}}</P>
</div>
<div class="col">
<P>{{att.value}}</P>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
<div class="row">
<div class="col-md-auto">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal"
data-bs-target="#searchModal">
افزودن کالا
</button>
<!-- 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"
aria-label="Close">
</button>
</div>
<div class="modal-body">
<form action="{% url 'compare_search' pk=object.pk %}" method="get">{% csrf_token %}
{{compare_form|crispy}}
<button class="btn btn-primary" type="submit">جستجو</button>
<br>
<br>
</form>
<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' pk=good.pk %}"
class="btn btn-primary stretched-link btn-sm">مشاهده جزئیات</a>
</div>
</div>
<br>
</div>
{% endfor %}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">بستن</button>
</div>
</div>
</div>
</div>
</div>
</div>
{% 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'),
]