Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: check a last character in string

I have this string doc.specalization.name from my views. If it does end with an s, I want to remove the s.

I tried this code but i get Could not parse the remainder: '('s')' from 'doc.specialization.name.endswith('s')'

 {% if doc.specialization.name.endswith('s') %}
        <h3> {{doc.specialization.name|slice:":-1"}} </h3>   
 {% endif %}
like image 879
James L. Avatar asked Nov 06 '25 12:11

James L.


1 Answers

Karthikr's answer is close. Ifequal is scheduled to be deprecated in future versions of Django, so you may want to use the method below. You also need to add a ':' after the '-1'.

{% if doc.specialization.name|default:""|slice:"-1:" == "s" %}
  <h3> {{ doc.specialization.name|slice:":-1" }} </h3>
{% else %}
  <h3> {{ doc.specialization.name }} </h3>
{% endif %}
like image 95
sv_rancher Avatar answered Nov 08 '25 09:11

sv_rancher