Django用内置方法实现简单搜索功能的方法
作者:汤圆儿2019 时间:2021-09-15 21:02:52
Model中分别提供了filter方法和icontains方法实现简单的搜索功能。
html页面中实现搜索框
模板api_test_manage.html中增加以下内容
<form method='get' action='/api_search/'>
{% csrf_token %}
<input type='search' name='api_test_name' placeholder='流程接口名称' required>
<button type='submit'>搜索</button>
</form>
views增加search函数,过滤数据
ApiTest/apiviews.py中增加函数
# 搜索功能
@login_required
def api_search(request):
username = request.session.get('user', '')
search_apiTestName = request.GET.get('api_test_name', '') # 获取name属性=api_test_name的值,即搜索框输入内容
apitest_list = ApiTest.objects.filter(apiTestName__icontains=search_apiTestName)
return render(request, 'apitest_manage.html', {'user': username, 'apitests': apitest_list})
urls文件中增加路径
AutoTestPlat/urls.py中增加连接映射
urlpatterns = [
path('admin/', admin.site.urls),
path('api_search/', apiviews.api_search),
搜索结果如下
来源:https://blog.csdn.net/weixin_38851970/article/details/107536745
标签:Django,内置方法,搜索
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL注入攻击成为新威胁将挑战操作系统安全
2009-03-16 15:13:00
Django项目如何给数据库添加约束
2023-08-10 14:49:39
![](https://img.aspxhome.com/file/2023/4/62014_0s.jpg)
Python 将json序列化后的字符串转换成字典(推荐)
2021-11-17 12:36:51
快速掌握ASP连接11种数据库的常用语法
2008-11-28 15:32:00
MySQL中两种快速创建空表的方式的区别
2008-12-17 14:34:00
在Yii框架中使用PHP模板引擎Twig的例子
2023-11-14 11:30:30
sqlserver 中时间为空的处理小结
2011-11-03 17:16:17
图文教程教你asp编译成dll组件
2010-07-16 13:16:00
![](https://img.aspxhome.com/file/UploadPic/20107/16/01-16s.gif)
Python3.7.0 Shell添加清屏快捷键的实现示例
2023-02-25 13:39:23
![](https://img.aspxhome.com/file/2023/6/86356_0s.png)
python字符串常用方法
2023-05-29 13:54:15
Python数据分析之 Pandas Dataframe修改和删除及查询操作
2023-08-03 23:01:13
![](https://img.aspxhome.com/file/2023/9/61699_0s.png)
python __add__()的具体使用
2022-11-29 07:49:22
![](https://img.aspxhome.com/file/2023/3/97673_0s.png)
对IPython交互模式下的退出方法详解
2021-08-04 10:03:11
python scipy求解非线性方程的方法(fsolve/root)
2022-01-06 15:46:00
![](https://img.aspxhome.com/file/2023/5/92215_0s.jpg)
MySQL 通过索引优化含ORDER BY的语句
2010-03-25 10:28:00
在SQL Server中使用索引的技巧
2009-02-24 17:50:00
简单谈谈Python流程控制语句
2023-03-12 12:34:25
详谈python3 numpy-loadtxt的编码问题
2021-08-28 06:42:09
在Python的一段程序中如何使用多次事件循环详解
2023-04-08 03:33:28
利用Python抢回在蚂蚁森林逝去的能量(实现代码)
2022-07-01 15:15:39
![](https://img.aspxhome.com/file/2023/6/90646_0s.jpg)