定义模型的models.py,示例代码如下:

from django.db import models

class Category(models.Model):
name = models.CharField(max_length=100) class Meta:
db_table = 'category' class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
category = models.ForeignKey('Category', on_delete=models.CASCADE, null=True) def __str__(self):
return "<(Article: id: %s,title: %s, content: %s)>" % (self.id, self.title, self.content) class Meta:
db_table = 'article'

1. startswith:大小写敏感的判断某个字段的值是否以某个值开始的。示例代码如下:

from .models import Article, Category
from django.http import HttpResponse def index(request):
articles = Article.objects.filter(title__startswith='hello')
print(articles)
print(articles.query)
return HttpResponse("success")
首先,查看数据库表中的数据如下:

打印出结果:

<QuerySet []>:返回的QuerySet为空。

SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE BINARY hello%. 需要注意的是这里执行的sql语句为LIKE BINARY hello%,LIKE BINARY代表的是区分大小写,hello%代表的是以hello为开头,%代表的是后面可以匹配任意多个字符。

2.istartswith: 大小写不敏感的判断某个字段的值是否以某个值开始的,示例代码如下:

from .models import Article, Category
from django.http import HttpResponse def index(request):
articles = Article.objects.filter(title__istartswith='hello')
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果如下:

<QuerySet [<Article: <(Article: id: 1,title: Hello, content: 你好)>>, <Article: <(Article: id: 2,title: Hello World, content: 大家好)>>]> : 查找出两条符合条件的文章。

SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title **LIKE hello%: 需要注意的是,这里将查询条件翻译成了sql语句为:article.title LIKE hello%,这里的LIKE代表的是不区分大小写,hello%代表的是以hello开头,后面可以匹配任意多个字符。

3.endswith: 大小写敏感的判断某个字段的值中是否含有某个值开始。示例代码如下:

from .models import Article, Category
from django.http import HttpResponse def index(request):
articles = Article.objects.filter(title__endswith='world')
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果如下:

<QuerySet []> 输出的结果为空的QuerySet。

SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE BINARY %world :这里的查询条件被翻译article.title LIKE BINARY %world,LIKE BINARY代表的是区分大小写的判断,%world代表的是以world为结束,前面可以匹配任意多个字符,如果可以满足条件就会返回。

4.iendswith: 不区分大小写判断某个字段的值中是否含有某个值,示例代码如下:

from .models import Article, Category
from django.http import HttpResponse def index(request):
articles = Article.objects.filter(title__iendswith='world')
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果如下所示:

<QuerySet [<Article: <(Article: id: 2,title: Hello World, content: 大家好)>>]>: 返回了一个满足条件的文章

SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE %world: 需要注意的是,这里为 LIKE,代表的是不区分大小写的判断,并且以world结尾,world前面可以匹配任意多个字符,满足条件才会被返回。

66.Python中startswith和endswith的使用的更多相关文章

  1. python 中startswith()和endswith() 方法

    startswith()方法 Python startswith() 方法用于检查字符串是否是以指定子字符串开头如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在 ...

  2. Python的startswith和endswith

    做文本处理的时候经常要判断一个文本有没有以一个子串开始,或者结束.Python为此提供了两个函数: S.startswith(prefix[, start[, end]]) -> bool 如果 ...

  3. Python中的startswith和endswith函数使用实例

    Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...

  4. python中strip、startswith、endswith

    strip(rm)用来删除元素内的空白符: rm对应要删除空白符的元素,当rm为空(strip())时删除所有元素的空白符 startswith.endswith用来查找开头或结尾条件的元素 例子: ...

  5. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  6. Python: 字符串开头或结尾匹配str.startswith(),str.endswith()

    问题 需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URLScheme 等等. 解决方案 1.检查字符串开头或结尾的一个简单方法是使用str.startswith() 或者是str ...

  7. 在Javascript中使用String.startsWith和endsWith

    在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...

  8. python startswith与endswith

    如果你要用python匹配字符串的开头或末尾是否包含一个字符串,就可以用startswith,和endswith比如:content = 'ilovepython'如果字符串content以ilove ...

  9. python startswith和endswith

    startswith判断文本是否以某个或某几个字符开始; endswith判断文本是否以某个或某几个字符结束; text = 'Happy National Day!' print text.star ...

随机推荐

  1. python获取最大、最小值

    1.获取数组极值,并返回索引 c = [-10,-5,0,5,3,10,15,-20,25]   print c.index(min(c)) # 返回最小值 print c.index(max(c)) ...

  2. postgres 删除外部表

    drop external table if exists tableName;

  3. Redar Chart_Study

    1.Select a theme 2.Experiment with visual customization 3.Edit groups and categories 4.Creat a scrip ...

  4. 设备树DTS 学习:1-有关概念

    背景 设备树在Linux驱动开发中是一种比较常用的架构. 参考:<设备树DTS使用总结> .<linux内核设备树及编译> Linux设备树 介绍 在Linux 2.6中,ar ...

  5. 强制找回GitLab管理员账户密码的方法

    为了开发运维工具,我们采用自行搭建的GitLab来管理所有代码.悲催的是最近忘记了管理员账户的密码,而且没有邮件服务器,因此无法接收密码找回的邮件,导致无法新建用户或者项目,这样一来,岂不就成为了一个 ...

  6. OBS Studio 24.0 RC1 发布 – 有大惊喜

    导读 对于那些使用OBS Studio进行跨平台直播和屏幕录制需求的人来说,OBS Studio 24.0即将推出,但首先发布的是他们的候选版本,以审查进入这一重大更新的新功能. OBS Studio ...

  7. Docker 学习之部署php + nginx(一)

    博主电脑系统是window 10 专业版的,所以在此记录下docker的基本使用方法. 参考地址: https://www.runoob.com/docker/docker-install-php.h ...

  8. Web项目设置编码格式

    1. 如果用的Tomcat ,请修改server.xml 中Connector URIEncoding="UTF-8" (GET请求时) 2. 用字符编码过滤器,设置默认编码方式为 ...

  9. 一个小证明(题解 P5425 Part1)

    所以这道题为什么可以这样做 嗯,我也不知道,不过我是来填坑的. \(Q\):为什么要把牛分成\(1\),\(1\)......\(N-K+1\)这样的\(K\)组呢? \(A\):我们设第\(i\)组 ...

  10. php 和 文本编辑器火狐的配置

    个人比较习惯的编辑器和浏览器配置 Sublime Ctrl+Shitf+P 输入 install 安装扩展: 点开菜单 -> view -> showConsole (或者按住 Ctrkl ...