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

Time:12-30

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 >
<p data-letters="{{account.variable_name}}" >
{{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

CodePudding user response:

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

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