Reputation: 145
I want to add a button to my html which can update automaticaly the status of client from 'active' to 'hide'. it will be an additional option for the user to choose to hide or to delete the object.
Model :
class Client(models.Model):
name = models.CharField(max_length=256)
status = models.CharField(max_length=25, default='active')
View :
def Client_List(request):
clients = Client.objects.filter(status='active')
context = {'clients':clients}
return render(request, 'sales/client_list.html', context)
class ClientDelete(DeleteView):
success_url = reverse_lazy('list_clients')
model = Client
def get(self, *a, **kw):
return self.delete(*a, **kw)
html :
<a class="btn" href="{% url 'delete_client' client.id %}">
<a class="btn" href="{% url 'hide_client' client.id %}">
Upvotes: 1
Views: 1075
Reputation: 477641
you should not use a GET request to create, update or remove entities. This violates the HTTP protocol specifications:
In particular, the convention has been established that the GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval. These methods ought to be considered "safe".
You can make a small form that makes a POST request, with:
<form method="post" action="{% url 'delete_client' client.id %}">
{% csrf_token %}
<button type="submit">delete</button>
</form>
as for hiding the object, you can make a simple view that looks like:
from django.shortcuts import redirect
def hide_client(request, pk):
Client.objects.filter(pk=pk).update(status='active')
return redirect(Client_list)
Upvotes: 1