1
I have a list of customers on the screen of the responsible person, in case he presses to deny the client, opens a modal with the field Reason for him to fill. I want to take what was typed in this field and save in the table Historico_profile in the database. However, it is not a form, as I do?
Also, I need to take to save also the denied customer id and that of the logged in responsible
py views.
def usuarios_autorizar(request, value=None, user_id=None):
user = Perfil.objects.get(pk=request.user.id)
title = "Usuarios nao autorizados"
grupo_responsavel = Grupo.objects.filter(responsavel=user.id)
usuarios = Perfil.objects.filter(grupos__in=grupo_responsavel, is_staff=False)
context = {'lista_usuarios': usuarios}
return render(request, 'usuarios_autorizar.html', context)
def negar(request, item_id):
# para negar
user = User.objects.get(id=item_id)
user.is_staff = False
user.save()
# historico
historico = Historico_Perfil.objects.create()
quem = User.objects.get(id=item_id)
print(quem.id)
historico.usuario = quem.id
historico.data = datetime.now()
historico.acao = "Negado"
historico.save()
return redirect("/cadastro/quantities")
py.models
class Historico_Perfil(models.Model):
acao = models.CharField(max_length=255)
motivo = models.CharField(max_length=255, blank=True)
data = models.DateField(default=datetime.now, blank=True)
usuario = models.ForeignKey(User)
responsavel = models.ForeignKey(User)
html
<table class="striped" id="cadastrados">
<thead>
<tr>
<th>Nome</th>
<th>Telefone</th>
<th>Ramal</th>
<th>Grupo</th>
<th>Autorizar</th>
<th>Negar</th>
</tr>
</thead>
{% for item in lista_usuarios %}
<tr>
<td>{{ item.first_name }}</td>
<td>{{ item.telefone }}</td>
<td>{{ item.ramal }}</td>
<td>{{ item.grupos }}</td>
<td id="autorizado"><a href="{% url 'cadastro:autorizar' item.id %}"
class="btn-floating waves-effect waves-light blue">
<i class="large material-icons">mode_edit</i>
</a>
</td>
<td id="negado"><a href="#modal1"
class="btn-floating waves-effect waves-light blue">
<i class="large material-icons">clear</i>
</a>
</td>
</tr>
</li>
<div id="modal1" class="modal">
<div class="modal-content">
<h4>Motivo</h4>
<textarea id="motivo" class="materialize-textarea"></textarea>
</div>
<div class="modal-footer">
<a href="{% url 'cadastro:negar' item.id %}" class="modal-action modal-close waves-effect waves-blue btn-flat blue">Enviar</a>
</div>
</div>
{% endfor %}
</table>
</div>
If you’re going to have a communication that way, you would have two ways: use a form inside the modal, or make an AJAX call. The second way brings the question of having to use Javascript. In the first way, it would just be a form with a button, and in it you would pass the information needed to process within your view.
– mazulo
Without using a form, you could try using an AJAX request.
– Neto Rangel