MarcoRos
MarcoRos

Reputation: 33

Django: in template take only first 2 letters of a variable taken by database

I'm new to Django and I'm trying to get only the first 2 letters of a Variable Name passed on by the database.

Here's my HTML code:

<td class="process-title">
<p data-letters="{{account.variable_name}}" class="text-accounts">
{{account.variable_name }}</p>
</td>

In the data-letters Tag I'd like to pass only 2 letters instead of the whole word.

This is my view.py:

def accounts(request):
    if request.user.is_authenticated:
        all_accounts = Accounts.objects.all().order_by('variable_name')
        context = {
            "accounts": all_accounts,
        }
        return render(request, 'engine/accounts.html', context)

I'm attaching a picture of the result I'm trying to achieve. How can I achieve this?

Thank you all enter image description here

Upvotes: 0

Views: 493

Answers (1)

TCFDS
TCFDS

Reputation: 622

You could just take 2 characters long substring of word. This can be done by

{{account.variable_name|slice:"0:2"}}

Upvotes: 3

Related Questions