Show message when there's no excerpt - Django templates

170 views Asked by At

I have this field on a Django template:

<p class="border_dotted_bottom">
                          {{ expert.description|slice:":300"  }}
<a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>

If this object (user) has no 'decription' (a text field) it shows the word 'None', I need to get rid of that, maybe if he doesn't have a 'description' then show a Simple text and then "read more"

So far, I've tried this:

        <p class="border_dotted_bottom">
            {{ % if expert.description >= 1 %}}
            {{ expert.description|slice:":300" }}
                {{% else %}}
            Éste usuario por el momento no tiene descripción
        <a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
         </p>

But it doesn't work, I think it's just a typo, or maybe something to do with the conditional I'm using here...

Anybody can shed some light on this?

Thanks in advance!

1

There are 1 answers

1
Mike Covington On BEST ANSWER

Your problem is with your if/else tags. You have this:

{{ % if ... %}}
  ...
{{% else %}}
  ...

First off, you need to surround if/else by {% %}, not {{% %}}. Secondly, you don't have an endif. An if/else block should look like this:

{% if ... %}
  ...
{% else %}
  ...
{% endif %}

Therefore, your desired block would look something like this:

<p class="border_dotted_bottom">
  {% if expert.description >= 1 %}
    {{ expert.description|slice:":300" }}
  {% else %}
    Éste usuario por el momento no tiene descripción
  {% endif %}
  <a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>

That being said, you should be able to simplify this using Django's built-in default tag or default_if_none tag, depending on whether you want to give a default if expert.description is equal to ''/None or only None:

<p class="border_dotted_bottom">
  {{ expert.description|default:"Éste usuario por el momento no tiene descripción"|slice:":300" }}
  <a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>