A
A
Andrey Kirdyashkin2021-10-21 17:00:42
Django
Andrey Kirdyashkin, 2021-10-21 17:00:42

How to change the date in a post using a button?

Unable to change

HTML date for record

<form method="POST">
{% if viewClients %}
                <tbody>
                    {% for el in viewClients %}
                <tr>
                    <td>{{ el.clients_name }}</td>
                    <td>{{ el.clients_tin }}</td>
                    <td>{{ el.clients_city }}</td>
                    <td>{{ el.clients_equipment }}</td>
                    <td>{{ el.clients_createdate|date:"j E Y" }}</td>
                    <td>{{ el.clients_deletedate|date:"j E Y" }}</td>
                    <td>
                        <div style="width: 135px;">
                            <form method="POST">
                                {% csrf_token %}
                                <button type="submit" name="clients_extend" value="{{ el }}" class="btn btn-primary"><i class="fa fa-clock-o" aria-hidden="true"></i></button>
                                <button type="submit" name="clients_sale" value="{{ el }}" class="btn btn-success"><i class="fa fa-check" aria-hidden="true"></i></button>
                                <button type="submit" name="clients_cancel" value="{{ el }}" class="btn btn-danger"><i class="fa fa-times" aria-hidden="true"></i></button>
                            </form>
                        </div>
                    </td>
                </tr>


MODELS.PY
class Clients(models.Model):
    clients_name = models.CharField('Название компании', max_length=50)
    clients_tin = models.CharField('ИНН', max_length=12, unique=True)
    clients_city = models.CharField('Город', max_length=30)
    clients_equipment = models.CharField('Оборудование', max_length=50)
    clients_dealer = models.ForeignKey(User, verbose_name='Дилер', on_delete=models.CASCADE, max_length=50)
    clients_createdate = models.DateField(verbose_name='Дата бронирования', default=(timezone.localtime(timezone.now()).strftime('%Y-%m-%d')), max_length=50)
    clients_deletedate = models.DateField(verbose_name='Дата снятия брони', default=(timezone.localtime(timezone.now()+timedelta(days=30)).strftime('%Y-%m-%d')), max_length=50)

    def __str__(self):
        return self.clients_name

    class Meta:
        verbose_name = 'Клиента'
        verbose_name_plural = 'Клиенты'


VIEWS.PY
def clients(request):
    error = ''
    added = ''
    fatalError = ''
    viewClients = Clients.objects.order_by('-clients_createdate').filter(clients_dealer__username=request.user)

    if request.method == 'POST' and 'clients_save' in request.POST:
        form = ClientsForm(request.POST)
        if form.is_valid():
            form.instance.clients_dealer = request.user
            form.save()
            added = 'Клиент успешно забронирован'
        else:
            error = 'Данный клиент забронирован'

    if request.method == 'POST' and 'clients_extend' in request.POST:
        SuccessExtendDate = Clients.objects.filter(clients_deletedate__lte=timezone.now()+timedelta(days=10))
        
        if SuccessExtendDate:
            Clients.objects.update(clients_deletedate=timezone.now()+timedelta(days=30))
            added = 'Вы успешно продлили бронирование для клиента на 30 календарных дней'
        else:
            error = 'Клиента можно продлить только за 10 дней до снятия брони'
    
    form = ClientsForm()

    data = {
        'form': form,
        'added': added,
        'error': error,
        'viewClients': viewClients
    }

    return render(request, 'main/clients.html', data)


Changes all records, I understand that I need to pass the id of the record, I did, but I probably didn’t understand how to work with it in views

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Kirdyashkin, 2021-10-22
@ANDYNEI

if SuccessExtendDate:
ClientExtendId = int(request.POST['clients_extend'])
Clients.objects.filter(id=ClientExtendId).update(clients_deletedate=timezone.now()+timedelta(days=30))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question