Question on a resembling function on truncatewords

25 views
Skip to first unread message

Kimitaka

unread,
Apr 12, 2014, 1:40:34 AM4/12/14
to django...@googlegroups.com
Hi, I started learning Django(also new to Python) and I would like to use it in Japanese. 

Since truncatewords doesn't seem working in non-English language, I tried to create a resembling function. 

I wrote the function in my Product class in my models.py:
class Product(models.Model):
title = models.CharField(max_length=220)
description = models.CharField(max_length=3000, null=True, blank=True)
price = models.DecimalField(max_digits=1000, decimal_places=2, null=True, blank=True)
slug = models.SlugField()
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=True, auto_now=False)
active = models.BooleanField(default=True)

def __unicode__(self):
return self.title
class Meta:
ordering = ['title',]

def shorten_words(self):
if len(self.description) > 20:
print self.desciption[0:20]
else:
print self.desciption


and I added a code in my products.html page:
{{ product.description.shorten_words() }}

And it's not working.. 
Could you give me some advices?
Thank you!

C. Kirby

unread,
Apr 12, 2014, 10:10:22 AM4/12/14
to django...@googlegroups.com
Don't use the parenthasesbin the template. Just call {{product.description.shorten_words}}

Kirby

Camilo Torres

unread,
Apr 12, 2014, 10:20:58 AM4/12/14
to django...@googlegroups.com
On Saturday, April 12, 2014 1:10:34 AM UTC-4:30, Kimitaka wrote:
I wrote the function in my Product class in my models.py:
class Product(models.Model):
title = models.CharField(max_length=220)
description = models.CharField(max_length=3000, null=True, blank=True)
price = models.DecimalField(max_digits=1000, decimal_places=2, null=True, blank=True)
slug = models.SlugField()
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=True, auto_now=False)
active = models.BooleanField(default=True)

def __unicode__(self):
return self.title
class Meta:
ordering = ['title',]

def shorten_words(self):
if len(self.description) > 20:
print self.desciption[0:20]
else:
print self.desciption


and I added a code in my products.html page:
{{ product.description.shorten_words() }}
Hello,

Should it be?:
{{ product.shorten_words }} 

Regards,
Camilo

Sanjay Bhangar

unread,
Apr 12, 2014, 10:29:26 AM4/12/14
to django...@googlegroups.com
This. And in the desciption() method, you want to do "return
self.description[0:20] and not print..." - print will just print the
output in the console at that point - you need to "return" the value
from the method.

So:

def shorten_words(self):
if len(self.description) > 20:
return self.desciption[0:20]
else:
return self.desciption

That, and {{ product.shorten_words }} in your template should do the trick.

All the best,
Sanjay

Alvin Lindstam

unread,
Apr 13, 2014, 12:14:23 PM4/13/14
to django...@googlegroups.com
And also:

1)
You have misspelled description in the shorten_words method. In you model field definition, and in your length check it's "description" but in the print/return statements it's spelled "desciption".

2)
The python slicing operator is clever enough to handle indexes that are out of bounds without errors, so self.description[0:20] would do nothing if len(self.description)<=20. Your method could simply be written as 
def shorten_words(self):
    return self.description[0:20]

3)
You seem to be truncating by character count instead of word count, have you tried using the built in template tag truncatechars?
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#truncatechars
Reply all
Reply to author
Forward
0 new messages