我正在尝试编写一段代码来查看文本条目是否包含50个或更多字符,如果是,则只显示50加省略号(...),否则它是否小于50然后只显示没有省略号的整个条目。
我目前的代码是:
from django.db import models
# Create your models here.
class Topic(models.Model):
"""A topic the user is learning about"""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of the model"""
return self.text
class Entry(models.Model):
"""Something specific learned about a topic"""
topic = models.ForeignKey(Topic)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
if text.len() >= 50:
"""Return a string represerntation of the model"""
return self.text[:50] + "..."
else:
return self.text
当我使用Python和Django运行时,我不断收到错误。我将如何解决此问题
答案 0 :(得分:1)
使用:
<GridScreen>:
label: label #referencing the label
rows: 2
Button:
text: 'this button \n is the root'
color: .8, .9, 0, 1
font_size: 32
on_press: root.btnpress() #calling the method
Label:
id: label
text: 'This is a label'
color: .9, 0, .5, .5
而不是:
if len(self.text) >= 50:
答案 1 :(得分:0)
from django.db import models
# Create your models here.
class Topic(models.Model):
"""A topic the user is learning about"""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of the model"""
return self.text
class Entry(models.Model):
"""Something specific learned about a topic"""
topic = models.ForeignKey(Topic)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
if len(self.text) >= 50:
"""Return a string represerntation of the model"""
return self.text[:50] + "..."
else:
return self.text