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

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 %}

Related

Error return truncatechars & safe by using Built-in template tags and filters

I try to Truncates the string and remove the html tags,
First, when I write it this way.
{{ post.context|safe }}
or
{{ post.context |truncatechars:100 }}
The left navigation bar shows normal.
But when I write this, this part of the HTML is gone.
{{ post.context |truncatechars:100|safe }}
But I can still find this Html in the source code.
So what can I do to get the correct results?thank you
If you just want to safely show content with HTML formatting.
{{ post.context|safe }}
If you truncate then some HTML tags may not get closed tag and you will get an irregular view.
If you want to strip HTML tags, you can strip by striptags and truncate characters using slice filters.
{{post.context|striptags|slice:':300'}}
Although it's kinda late to answer if you want to show the safe code with tags then use {{ post.context|truncatewords_html:30|safe }} or {{ post.content|truncatechars_html:100|safe }}. This won't break your code and will display your desired content.
No need to struggle around. You need to combine truncate, safe, and striptags
Respect the order below:
{{ string_variable|striptags|safe|truncate(100) }}
Does that help you?

How to remove line break after a tag

I use Django template system to do code generation (not only for HTML). I'm somehow troubled by redundant line breaks in django templates.
Here is an example. The template is as below:
// something
{% for element in elements %}
Element: {{ element.name }},
{% endfor %}
// something else
The rendered output will be:
// something
Element: foo
Element: bar
// something else
Expected rendered output should be:
// something
Element: foo
Element: bar
// something else
After googled a bit, I know I can use {% spaceless %} to remove any white spaces in rendered output. It is quite useful for HTML, but will not work for other languages. My current solution is to add a special string after a tag and replace them with empty string in output.
Is there any better solution to remove line break after a tag?
For your production environment you might consider minifying your html to get that little bit more performance. For example using https://pypi.python.org/pypi/django-htmlmin.
If you are only interested in the esthetics, then the .strip function as noted by e-nouri is probably your best answer.
You can use .strip on your attributes to strip/trim.
// something
{% for element in elements %}
Element: {{ element.name.strip }},
{% endfor %}
// something else
The line breaks in code shouldn't matter, but you can avoid them when you put everything in one line, which doesn't look that good anymore:
{% for element in elements %}Element: {{ element.name.strip }},{% endfor %}

django template string - replace line break with line space

I have a string in my django1.4 template that I want to replace the line breaks with a whitespace. I only want to replace the line breaks with a single white space in the template string.
So far all my searces on django docs, Google and SO have not given me an answer.
Here is my string in my template:
{{ education_detail.education_details_institution_name|safe|truncatechars:20|striptags }}
When I have the following string saved:
University
Bachelor of Something
2008 - 2010
The string in the django template is rendered as:
UniversityB...
I want to replace the line break with a space between the yB like so:
University B...
How would I do this?
Here is the custom filter code that I finally got operational:
from django import template
register = template.Library()
#register.filter(name='replace_linebr')
def replace_linebr(value):
"""Replaces all values of line break from the given string with a line space."""
return value.replace("<br />", ' ')
Here is the call on the template:
{{ education_detail.education_details_institution_name|replace_linebr }}
I hope that this will help somebody else.
You can rely on built-in truncatechars filter's behavior to replace newlines with spaces. All you need is to pass a length of the string as an argument, so that you would not see your string shortened:
{% with value|length as length %}
{{ value|truncatechars:length }}
{% endwith %}
This is a bit hacky, but uses only built-in filters.
You can always write a custom filter if you need this kind of functionality to be reusable.

Make the first letter uppercase inside a django template

I am pulling a name from a database which is stored as myname. How do I display this inside a Django template as Myname, with the first letter being in uppercase.
Using Django built-in template filter called title
{{ "myname"|title }}
I know it's a bit late, but you can use capfirst:
{{ "waiting for action"|capfirst }}
This will result into "Waiting for action"
This solution also works if you have multiple words (for example all caps):
{{ "ALL CAPS SENTENCE"|lower|capfirst }}
This will output "All caps sentence".
The title filter works fine, but if you have a many-words string like: "some random text", the result is going to be "Some Random Text". If what you really want is to uppercase only the first letter of the whole string, you should create your own custom filter.
You could create a filter like this (follow the instructions on how to create a custom template filter from this doc - it's quite simple):
# yourapp/templatetags/my_filters.py
from django import template
register = template.Library()
#register.filter()
def upfirstletter(value):
first = value[0] if len(value) > 0 else ''
remaining = value[1:] if len(value) > 1 else ''
return first.upper() + remaining
Then, you should load the my_filters file at your template, and use the filter defined there:
{% load my_filters %}
...
{{ myname|upfirstletter }}
It worked for me in template variable.
{{ user.username|title }}
If the user is "al hasib" then the it will return "Al Hasib"
or
{{ user.username|capfirst }}
If user is 'hasib' then the last one will return "Hasib"
Both look something like same but there's some differences.
use
{{"myname"|title}}
this will make the fist letter of each word capital
Just use {{myname | capfirst}}
In Django the template filter capfirst capatialize the first letter of a given string.

Django templates - split string to array

I have a model field, which stores a list of URLs (yeah, I know, that's wrong way) as url1\nurl2\nurl3<...>. I need to split the field into an array in my template, so I created the custom filter:
#register.filter(name='split')
def split(value, arg):
return value.split(arg)
I use it this way:
{% with game.screenshots|split:"\n" as screens %}
{% for screen in screens %}
{{ screen }}<br>
{% endfor %}
{% endwith %}
but as I can see, split doesn't want to work: I get output like url1 url2 url3 (with linebreaks if I look at the source). Why?
Django intentionally leaves out many types of templatetags to discourage you from doing too much processing in the template. (Unfortunately, people usually just add these types of templatetags themselves.)
This is a perfect example of something that should be in your model not your template.
class Game(models.Model):
...
def screenshots_as_list(self):
return self.screenshots.split('\n')
Then, in your template, you just do:
{% for screen in game.screenshots_as_list %}
{{ screen }}<br>
{% endfor %}
Much more clear and much easier to work with.
Functionality already exists with linkebreaksbr:
{{ value|linebreaksbr }}
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#linebreaksbr
Hm, I have partly solved this problem. I changed my filter to:
#register.filter(name='split')
def split(value, arg):
return value.split('\n')
Why it didn't work with the original code?
I wanted to split a list of words to get a word count, and it turns out there is a filter for that:
{{ value|wordcount }}
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#wordcount
Apart from whether your original solution was the right approach, I guess the original code did not work because the meaning of the \n is not the same in Python code as it is in HTML: In Python code it means the escaped newline character, in HTML it is just the two separate characters \ and n.
So passing as input parameter \n from the HTML template to the Python code is equivalent to splitting on the Python string \\n: a literal \ followed by a n.