Django template tag left / right - django

Given text. Is there a django template tag to do:
text = 'hello there'
text[:3] ==> 'hel'
text[-3:] ==> 'ere'
Something like:
{{ text|left:3 }}
If not I can build a template tag for it but was wondering if it's already provided.

For a standard django template, you can use the slice template tag:
{{ text|slice:":3" }}
will only display the first three characters and
{{ text|slice:"-3:" }}
will only display the last three characters.
Essentially this template tag works just like the standard python slice syntax and just like standard slicing, it will work on both lists and strings. The docs only describe its use on lists so I can see why you'd be confused about this. Hope this helps!
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#slice

Related

how can I truncate text and remove html tags in django template

I've used truncatechars but if text contains a html tag, it will show the tag for me.
{{content.text|safe|truncatechars:140}}
for example displays something like this:
<p>hi</p>
I want to remove p tag.
Updated along with question:
To completely remove the tags before truncating, use:
{{ content.text|striptags|truncatechars:140 }}
Original answer:
To ensure the tags are broken correctly, you'll want to use the truncatechars_html template filter.

String manipulation in Django templates

Imagine the context variable {{ url }} outputs www.example.com/<uuid-string> where <uuid-string> is different every time, whereas the former part of the URL stays the same.
Can one change the output of {{ url }} to instead www.forexample.com/<uuid-string>, via solely manipulating the string in the template and without involving views.py (which I know is the better way to do it, but that's not the question).
An illustrative example would be great.
read about filters and templatetags - they are a methods that allows you to perform some actions on variables in templates.
You can also create your own tags and filters that allow you to perform action non-built into Django template language
Simple example of such filter:
#in templatetags.py
#register.filter(name='duplicate')
def duplicate(value):
return value*2
#in your template
<p> {{ url|duplicate }} </p>
You can find more examples here. Also there you will find tutorial how to use and create them

How can I truncate a text and use the second part of the text?

I'm researching for a little problem.
I get on my Django template a text. The point is truncate that text, and add the second part of the text in other tag.
I read about:
{{ value|truncatechars_html:x }}
It can works, but I can't use the second part of truncated text.
If someone has an idea...Thanks!!!!
Use slice - see docs here. Strings in python are also lists of characters, so you can do something like this:
First part is {{ value|slice:":x" }}
Second part is {{ value|slice:"x+1:" }}
HOWEVER, if you need to keep some html code, slice won't do it. For that, you will need to write your own custom tag (you can look into the code from truncate chars) which returns two values - one for each part of your string.
Assuming your filter returns something like this:
def your_truncate_filter(value, arg):
... # your code for splitting
data = {}
data['first'] = "....." # first part of string with html tags !!!
data['second'] = "....." # second part of string
return data
you can access it in your template like this:
{% with parts=value|your_truncate_filter:x %}
{{ parts.first }}
{{ parts.second }}
{% endwith %}

Django / Cactus and truncate words with markdown

Trying to use {{ post|truncatewords:"100" }} for a teaser in Cactus. The post is using the markdown filter and truncate words is showing the html. I also tried {{ post|truncatewords:"100"|markdown }} which does seem like it is attempting to format correctly, however line breaks and body tags still show.
Example Output:
{'body': u'\n\n
Cactus templates are set up using the Django Template Language. Django\u2019s template language is both powerful and easy to use. A template is simply a HTML file. Let\'s take a look at how to use the blog template.
\n\n
General file str...
Seems like this should be pretty straight forward but I cannot figure out what I am doing wrong.
Any help would be greatly appreciated.
Thanks.
Looks like post is a dictionary, and the post body you want to show is under the body key. You probably want something like:
{{ post.body|markdown|truncatewords_html:100 }}

remove initial charachter from string in django template

I have a variable in django template who prints "Due 17 Dec, MATHS: math homework" I want only MATHS: math homework , how could I do that in django template.
To remove the initial character (per your question title),
{{ my_string|slice:':1' }}
But your example removes more than that
{{ my_string|slice:':12' }}
If the processing is more complex than that, do it in the view, or write your own custom template filter.
Django deliberately discourages significant processing in templates.