【发布时间】:2011-09-12 17:55:10
【问题描述】:
我认为有几行代码可以获取查询参数,然后根据该参数进行过滤。
search = request.GET.get('search', None)
if search:
accounts = UserProfile.objects.filter(fullname__icontains=search).order_by('fullname')
else:
accounts = UserProfile.objects.all().order_by('fullname')
出于某种奇怪的原因,在我的本地计算机上,似乎当我提供 localhost/accounts/admin/ 或 localhost/accounts/admin/?search= 之类的 URL 时,它工作正常——但是,在我的生产服务器上,它似乎认为搜索是一个空字符串,然后它通过 if/then 条件返回一个空的查询集。 GET:<QueryDict: {u'search': [u'']}>
为什么python似乎认为这个空字符串是“某物”?
>>> x = ''
>>> if x:
... print "Exists"
... else:
... print "None"
...
None
>>> x = u''
>>> if x:
... print "exists"
... else:
... print "None"
...
None
【问题讨论】:
标签: python django django-queryset