在Django中进行通配符搜索

编程入门 行业动态 更新时间:2024-10-26 06:38:15
本文介绍了在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中进行通配符搜索

本文发布于:2023-10-18 23:47:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505776.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!