2011-03-08 8 views
55

Django truncatewords टेम्पलेट टैग है, जो दिए गए शब्द गणना में टेक्स्ट को काटता है। लेकिन truncatechars जैसे कुछ भी नहीं है। दिए गए चार लंबाई सीमा पर टेम्पलेट में पाठ को काटने का सबसे अच्छा तरीका क्या है? , http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filtersटेक्स्ट को छिड़काव करने के लिए Django टेम्पलेट टैग

कैसे truncatewords में django.utils.text

उत्तर

102

यह हाल ही में Django 1.4 में जोड़ा गया है। उदा .:

{{ value|truncatechars:9 }} 

See doc here

+2

"truncatechars" नवीनतम Django 1.9 पर भी काम कर रहा है –

1
+0

धन्यवाद, लेकिन यह काफी है कि मैं क्या जरूरत नहीं है। – grigy

+0

कैसा है? इसका उपयोग तारों को टुकड़ा करने के लिए किया जा सकता है। –

+0

दस्तावेज़ीकरण कहता है कि यह सूचियों के लिए है। इसके अलावा यह छंटनी पाठ के लिए "..." संलग्न नहीं करता है। – grigy

2

आप एक कस्टम टेम्पलेट फिल्टर लिखना चाहिए Django एकहै इस के लिए:

{{ value|truncatechars:9 }} 
46
{{ value|slice:"5" }}{% if value|length > 5 %}...{% endif %} 

अद्यतन

बनाया गया है संस्करण 1.4 के बाद से पर एक नज़र डालें:

+0

के लिए टेम्पलेट टैग ट्रंकेटर्स को बिछाया है क्योंकि मैं प्री-1.4 डीजेगो के साथ काम कर रहा हूं, यह वही है जो मुझे चाहिए था। धन्यवाद! –

+2

यहां तक ​​कि बेहतर, '& hellip;' का उपयोग करें ... – mlissner

+0

['truncatechars'] (https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#truncatechars) फ़िल्टर एक अंडाकार चरित्र जोड़ता है डिफ़ॉल्ट रूप से। – cw1998

9

मैं भी (के अंतिम शब्द) (छोटा) स्ट्रिंग के अंत करने के लिए अपने खुद के टेम्पलेट फिल्टर बनाया है, कि जोड़ने "...":

from django import template 
register = template.Library() 

@register.filter("truncate_chars") 
def truncate_chars(value, max_length): 
    if len(value) > max_length: 
     truncd_val = value[:max_length] 
     if not len(value) == max_length+1 and value[max_length+1] != " ": 
      truncd_val = truncd_val[:truncd_val.rfind(" ")] 
     return truncd_val + "..." 
    return value 
+0

इसका डिफ़ॉल्ट अब यह django है। –

0

जोड़ना एक "ट्रंकेट" फ़िल्टर 4 साल के लिए एक फीचर अनुरोध था, लेकिन अंत में मैं ट्रंक में उतर गया, जहां तक ​​मैं https://code.djangoproject.com/ticket/5025 समझता हूं - इसलिए हमें अगली रिलीज या ट्रंक का उपयोग करना होगा।

0

आप अपने स्वयं के कस्टम टेम्पलेट टैग बनाने util Truncator उस में Django उपयोग करने के लिए विचार करने के लिए पसंद करते हैं।

>>> from django.utils.text import Truncator 
>>> Truncator("Django template tag to truncate text") 
<Truncator: <function <lambda> at 0x10ff81b18>> 
>>>Truncator("Django template tag to truncate text").words(3) 
u'Django template tag...' 
Truncator("Django template tag to truncate text").words(1) 
u'Django...' 
Truncator("Django template tag to truncate text").chars(20) 
u'Django template t...' 
Truncator("Django template tag to truncate text").chars(10) 
u'Django ...' 

तो फिर तुम यह एक टेम्पलेट टैग में डाल सकते हैं: निम्नलिखित एक नमूना उपयोग है

from django import template 
from django.utils.text import Truncator 

register = template.Library() 

@register.filter("custom_truncator") 
def custom_truncator(value, max_len, trunc_chars=True): 
    truncator = Truncator(value) 
    return truncator.chars(max_len) if trunc_chars else truncator.words(max_len) 
संबंधित मुद्दे