Answer the question
In order to leave comments, you need to log in
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>
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 = 'Клиенты'
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)
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question