从 Django 模型 object 中的字符串中减去子字符串
Substract a substring from a string in a model object in Django
我正在用 Django 构建一个博客网站,对于博客上的 post,我想要 2 个页面,第一页是显示所有 post 的页面作为列表,第二个将是特定 post 的页面,其中包含所有详细信息。在第一页上,我想显示每个 post 的标题、日期和 post 文本的一部分。我认为我可以向 post 模型添加另一个字段,该字段将包含洞 post 文本中的子字符串。我的 post 模型是这样的:
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=500)
content = models.TextField()
display_content = models.TextField(blank=True)
tags = models.CharField(max_length=100)
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
我希望每个 post display_content 字段都包含内容字段的前 167 个字符 +“...”,但我不确定是否必须这样做直接在 class Post 上实现一些东西,也许是一个函数,或者如果我需要在使用 posts.
呈现此页面的视图函数上进行此操作
您可以在 __str__
方法中定义逻辑,但最好定义一个您以后可以重用的实用函数:
def shorten(text, max_len=167):
if len(text) <= max_len:
return text
else:
return <b>'{}…'.format(text[:max_len])</b>
请注意 ellipsis character ('…'
) [wiki] 是单个字符,通常不是三个单独的点。
然后我们可以在__str__
方法中使用这个:
from django.conf import settings
class Post(models.Model):
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
title = models.CharField(max_length=500)
content = models.TextField()
display_content = models.TextField(blank=True)
tags = models.CharField(max_length=100)
date_posted = models.DateTimeField(auto_now_add=True)
def __str__(self):
return <b>shorten(self.title)</b>
对于模板,Django 已经有了 |truncatechars
template filter。因此,如果您计划在模板中 render ,则无需在模型中实现此逻辑。您可以使用以下方式呈现标题:
{{ mypost.title<b>|truncatechars:167</b> }}
这更有意义,因为 Django 模型不应该关心如何 呈现 数据,模型处理 存储 ,和修改数据。
Note: It is normally better to make use of the settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use the User
model [Django-doc] directly. For more information you can see the referencing the User
model section of the documentation.
Note: Django's DateTimeField
[Django-doc]
has a auto_now_add=…
parameter [Django-doc]
to work with timestamps. This will automatically assign the current datetime
when creating the object, and mark it as non-editable (editable=False
), such
that it does not appear in ModelForm
s by default.
我正在用 Django 构建一个博客网站,对于博客上的 post,我想要 2 个页面,第一页是显示所有 post 的页面作为列表,第二个将是特定 post 的页面,其中包含所有详细信息。在第一页上,我想显示每个 post 的标题、日期和 post 文本的一部分。我认为我可以向 post 模型添加另一个字段,该字段将包含洞 post 文本中的子字符串。我的 post 模型是这样的:
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=500)
content = models.TextField()
display_content = models.TextField(blank=True)
tags = models.CharField(max_length=100)
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
我希望每个 post display_content 字段都包含内容字段的前 167 个字符 +“...”,但我不确定是否必须这样做直接在 class Post 上实现一些东西,也许是一个函数,或者如果我需要在使用 posts.
呈现此页面的视图函数上进行此操作您可以在 __str__
方法中定义逻辑,但最好定义一个您以后可以重用的实用函数:
def shorten(text, max_len=167):
if len(text) <= max_len:
return text
else:
return <b>'{}…'.format(text[:max_len])</b>
请注意 ellipsis character ('…'
) [wiki] 是单个字符,通常不是三个单独的点。
然后我们可以在__str__
方法中使用这个:
from django.conf import settings
class Post(models.Model):
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
title = models.CharField(max_length=500)
content = models.TextField()
display_content = models.TextField(blank=True)
tags = models.CharField(max_length=100)
date_posted = models.DateTimeField(auto_now_add=True)
def __str__(self):
return <b>shorten(self.title)</b>
对于模板,Django 已经有了 |truncatechars
template filter。因此,如果您计划在模板中 render ,则无需在模型中实现此逻辑。您可以使用以下方式呈现标题:
{{ mypost.title<b>|truncatechars:167</b> }}
这更有意义,因为 Django 模型不应该关心如何 呈现 数据,模型处理 存储 ,和修改数据。
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.
Note: Django's
DateTimeField
[Django-doc] has aauto_now_add=…
parameter [Django-doc] to work with timestamps. This will automatically assign the current datetime when creating the object, and mark it as non-editable (editable=False
), such that it does not appear inModelForm
s by default.