'<' не поддерживается между экземплярами 'int' и 'str' в представлении поиска Django
Я только вчера обнаружил эту ошибку, не знаю, что случилось, все работало нормально, пока я полностью не протестировал сайт
TypeError at /home/search_view/
'<' not supported between instances of 'int' and 'str'
Request Method: GET
Request URL: http://127.0.0.1:8000/home/search_view/?csrfmiddlewaretoken=MzGKJGMz7C3uoJgFIILHdZcaLihBfOHQbLGNzyEi9X0HzptGqNbOREi5AS5cQZxo&q=bahrain
Django Version: 3.2.8
Exception Type: TypeError
Exception Value:
'<' not supported between instances of 'int' and 'str'
Exception Location: C:\Users\moham\Desktop\Tm471_final\tm471\travel_guide\views.py, line 174, in get_queryset
Python Executable: C:\Users\moham\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.6
Python Path:
['C:\\Users\\moham\\Desktop\\Tm471_final\\tm471',
'C:\\Users\\moham\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\moham\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\moham\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\moham\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\moham\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time: Fri, 02 Dec 2022 13:33:52 +0000
эта ошибка возникает, когда я пытаюсь искать с любыми словами как в этом примере
views.py
class SearchView(ListView):
template_name = 'travel_guide/view_list.html'
paginate_by = 20
count = 0
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['count'] = self.count or 0
context['query'] = self.request.GET.get('q')
return context
def get_queryset(self):
request = self.request
query = request.GET.get('q', None)
if query is not None:
country_results = country.objects.search(query)
city_results = city.objects.search(query)
articles_results = articles.objects.search(query)
print(query)
# combine querysets
queryset_chain = chain(
country_results,
city_results,
articles_results,
)
qs = sorted(queryset_chain,
key=lambda instance: instance.pk,
reverse=True)
self.count = len(qs) # since qs is actually a list
return qs
return country.objects.none()
HTML файл
{% extends 'base.html'%}
{% load class_name %}
{% block content %}
<div class="container">
<div class='row title-row my-5'>
<div class='col-12 py-0'>
<h3 class='my-0 py-0'>{{ count }} results for <b>{{ query }}</b></h3>
</div>
</div>
{% for object in object_list %}
{% with object|class_name as item %}
{% if item == 'country' %}
<div class="search-res">
<div class='row s-color p-3 med-text'>
<div class='col-12 '>
<a href="{% url 'country_data' object.ckey object.countryName %}">country data: {{ object.countryName }} </a>
</div>
</div>
{% elif item == 'city' %}
<div class='row s-color p-3 med-text'>
<div class='col-12 '>
<a href="{% url 'city_detail' object.cityID %}">city data: {{ object.cityName }}</a>
</div>
</div>
{% elif item == 'articles' %}
<div class='row s-color p-3 med-text'>
<div class='col-12 '>
<a href="{% url 'articles_detail' object.id %}">article Item: {{ object.articleHeader }}</a>
</div>
</div>
{% else %}
<div class='row s-color p-3 med-text'>
<div class='col-12 col-lg-8 offset-lg-4 '>
<a href='{{ object.get_absolute_url }}'>{{ object }} | {{ object|class_name }}</a>
</div>
</div>
</div>
{% endif %}
{% endwith %}
{% endfor %}
{% endblock content %}
models.py
class customCountryQuerySet(models.QuerySet):
def search(self, query=None):
qs = self
if query is not None:
or_lookup = (Q(countryName__icontains=query) |
Q(countryBrief__icontains=query)|
Q(cultures__icontains=query)
)
qs = qs.filter(or_lookup).distinct() # distinct() is often necessary with Q lookups
return qs
class country_Manager(models.Manager):
def get_queryset(self):
return customCountryQuerySet(self.model, using = self._db)
def search(self, query=None):
return self.get_queryset().search(query=query)
и здесь форма поиска я использую name='q', чтобы получить результаты запроса
<form class="d-flex index" method="GET" action="{% url 'search_view' %}">
<div class="search-bar">
{% csrf_token %}
<img class="icon" alt="search icon" src="{% static 'travel_guide/img/search.svg' %}" />
<input
class="search-box"
type="search"
placeholder="Search for your way" name="q"
/>
<!-- <button class="btn btn-outline-secondary" type="submit">search</button> -->
</div>
</form>
Я ожидаю, что результаты будут отображаться нормально без этих ошибок Я провел небольшое исследование, но я не нашел полезного решения, поэтому я надеюсь, что вы поможете мне .