本文介绍了在Django中进行通配符搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何在Django中进行通配符搜索。如果我要从数据库中的列表中过滤用户名,怎么可能显示具有这些确切用户名或其中一部分的过滤数据??
How can we do a wildcard searching in Django. If i am filtering username from a list in database, how is it possible to display the filtered data with those exact usernames or part of it.?
def filter(request): val3='' if request.GET.has_key('choices'): val2=request.GET.get('choices') if request.GET.has_key('textField'): val3=request.GET.get('textField') if request.POST: val2=request.POST.get('choices') val3=request.POST.get('textField') if val2=='Designation': newData = EmployeeDetails.objects.filter(designation=val3) flag=True elif val2=='Name': newData = EmployeeDetails.objects.filter(userName=val3) flag=True elif val2=='EmployeeID': newData = EmployeeDetails.objects.filter(employeeID=val3) flag=True elif val2=='Project': newData = EmployeeDetails.objects.filter(project=val3) flag=True elif val2=='DateOfJoin': newData = EmployeeDetails.objects.filter(dateOfJoin=val3) flag=True else: return HttpResponseRedirect('/employeeList/')这是我的功能用于过滤。现在使用准确的单词进行过滤。我想显示用户名,即使其中一部分用于过滤也是如此。请帮我解决这个问题,因为我是Django新手
This is my function for filtering. Now its filtering with exact words. I want to display the userNames even if part of it is given for filtering. Please help me to solve this as i am new with Django
推荐答案请尝试使用不太严格的过滤器,例如__contains:
Try using a less strict filter, like __contains:
elif val2=='Name': newData = EmployeeDetails.objects.filter(userName__contains=val3) flag=True文档: docs.djangoproject/en/dev/ref/models/querysets/#contains
更多推荐
在Django中进行通配符搜索
发布评论