gpt4 book ai didi

Django - 过滤器(__icontains)不起作用

转载 作者:行者123 更新时间:2023-12-04 16:08:43 27 4
gpt4 key购买 nike

请帮忙解决问题:

class SearchResultView(TemplateView):
template_name = "search_result.html"

def get_context_data(self, **kwargs):
context = super(SearchResultView, self).get_context_data(**kwargs)
location = self.request.GET['location']
locations_searched = Location.objects.filter(name__icontains=location)
context['locations_searched'] = locations_searched
return context

class AdvancedSearchForm(forms.Form):
location = forms.CharField(label=u"Локация:")

在位置字段中输入大写单词时,我可以在 View 中看到结果,但是当不大写时 - View 中不显示任何内容

谢谢!

最佳答案

我猜您正在使用 SQLite 数据库并且输入中有一些非 ASCII 字符。来自 SQLite FAQs :

Case-insensitive matching of Unicode characters does not work. The default configuration of SQLite only supports case-insensitive comparisons of ASCII characters. The reason for this is that doing full Unicode case-insensitive comparisons and case conversions requires tables and logic that would nearly double the size of the SQLite library.



Django 的 documentation还提到了这一点:

For all SQLite versions, there is some slightly counter-intuitive behavior when attempting to match some types of strings. These are triggered when using the iexact or contains filters in Querysets...

关于Django - 过滤器(__icontains)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37389181/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com