D
D
DmSS19972022-02-27 14:53:45
Django
DmSS1997, 2022-02-27 14:53:45

How to hide the last digits of a phone number taken from the database on the server side?

I have phone numbers stored in a database in text format. When displaying them on the page, you need to hide the last two digits behind an asterisk, it is important to do this from the server side so that it is not possible to somehow parse or see the html code in the browser.

model.py

class DailyOrders(models.Model):
    phone = models.CharField(max_length=25, verbose_name='Номер телефона')
    name_of_client = models.CharField(max_length=100, blank=True, null=True, verbose_name='Имя')


I tried to do it myself like this:
views.py
class QuickOrdersPage(ListView):
    model = DailyOrders
    template_name = 'Catalog/quick_orders.html'
    context_object_name = 'orders'
    allow_empty = True
    paginate_by = 10

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)

        def hide_phone(self):
            allOrders = DailyOrders.objects.all()
            for oneOrder in allOrders:
                clientPhone = oneOrder.phone
                clientPhone = clientPhone[:-2]
                clientPhone += '**'
                return clientPhone
        context['clientPhone'] = hide_phone(self)
        return context


{% for order in orders%}
 <div class="modal-quick__row">
       <div class="modal-quick__column first__column">
              <div class="modal-quick__title">Имя собственника:</div>
       </div>
        <div class="modal-quick__column second__column">
               <div class="modal-quick__text">{{order.name_of_client}}</div>
          </div>
</div>
<div class="modal-quick__row">
         <div class="modal-quick__column first__column">
                <div class="modal-quick__title">Телефон:</div>
          </div>
          <div class="modal-quick__column second__column">
                <div class="modal-quick__text" id="client__phone">{{clientPhone}}</div>
           </div>
</div>
{% endfor %}

But it doesn't work. The number is hidden, but only one number is shown everywhere. And I understand why. But I don't understand how to fix this or how to do it differently, please tell me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2022-02-27
@DmSS1997

This is the job of the filter .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question