106π
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.
16π
Functionality already exists with linkebreaksbr
:
{{ value|linebreaksbr }}
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#linebreaksbr
- [Django]-Cancel an already executing task with Celery?
- [Django]-Django queryset filter β Q() | VS __in
- [Django]-How to show a many-to-many field with "list_display" in Django Admin?
8π
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?
- [Django]-Django storages: Import Error β no module named storages
- [Django]-How to change a django QueryDict to Python Dict?
- [Django]-Is Django for the frontend or backend?
1π
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
- [Django]-How to test Django's UpdateView?
- [Django]-Set Django IntegerField by choices=β¦ name
- [Django]-Django Rest Framework: Access item detail by slug instead of ID
1π
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
.
- [Django]-Foreign key from one app into another in Django
- [Django]-How do you configure Django to send mail through Postfix?
- [Django]-Django queries: how to filter objects to exclude id which is in a list?