Home > Back-end >  django convert list to string
django convert list to string

Time:11-03

I am trying to convert a list to string.

CodePudding user response:

As this variable is a string of a list. You can either just use a simple if tag to condition the value. Like so:

{% if text.1 != "[]" %}
   {{ text.1 }}
{% endif %}

OR

You could create a custom tag to filter out these type of values. Follow the tutorial here. The function and tag would look something like this:

@register.simple_tag
def replace_empty_list_string(value):
    if value == "[]":
         return '' #return what you want an empty list to look like here (e.g. '<empty>')
    return value
{{ text.1|replace_empty_list_string }}

CodePudding user response:

okay I just used ''.join to convert a list to string and it's removing the[].

thx again I didn't know the tags, it is very interesting.

  • Related