定义模型的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. redis提供的持久化机制(rdb,aof)

    Redis提供的持久化机制 Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等优势.它虽然起步较晚,但发展却十分迅速. 近日,R ...

  2. 多门店4s管理系统

    下载 系统登录用户名与密码:manage/123456

  3. leetcode142 Linked List Cycle II

    """ Given a linked list, return the node where the cycle begins. If there is no cycle ...

  4. k-近邻算法python代码实现(非常全)

    1.k近邻算法是学习机器学习算法最为经典和简单的算法,它是机器学习算法入门最好的算法之一,可以非常好并且快速地理解机器学习的算法的框架与应用.它是一种经典简单的分类算法,当然也可以用来解决回归问题.2 ...

  5. 六 一些常用类:Random、BigInteger、BigDecimal、DecimalFormat

    常用类:

  6. linux下mysql允许远程连接

    1. MySql安装教程 https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html 默认情况下mysq的 roo ...

  7. LeetCode242 有效的字母异位词(Java字符数组排序&自定义排序记录)

    题目: 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词.   示例 1: 输入: s = "anagram", t = "nagaram& ...

  8. 你必须知道的.Net 8.2.2 本质分析

    1 .Equals  静态方法  Equals 静态方法实现了对两个对象的相等性判别,其在 System.Object 类型中实现过程可以表 示为: public static bool Equals ...

  9. 2-10 就业课(2.0)-oozie:10、伪分布式环境转换为HA集群环境

    hadoop 的基础环境增强 HA模式 HA是为了保证我们的业务 系统 7 *24 的连续的高可用提出来的一种解决办法,现在hadoop当中的主节点,namenode以及resourceManager ...

  10. SpringBoo#Mybatis多个数据源配置,Sqlite&Mysql

    第一步:排除数据源的自动配置类: @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 第二步:定义好两个数据源的 ...