1👍
✅
You should specify a parameter where to put the notes.id
parameter, for example:
path('projects/checklist/delete_notes/<int:note_id>/', delete_notes, name='delete_notes'),
Such view however should only be triggered with a POST or DELETE request, not with a GET request: a GET request should only be used to retrieve data, not to alter entities.
So you protect the view with:
from django.views.decorators.http import require_http_methods
@login_required
@require_http_methods(['POST', 'DELETE'])
def delete_notes(request, note_id):
Note.objects.filter(id=note_id).delete()
return redirect('teams')
and in the template you should work with a "mini-form":
<form method="post" action="{% url 'delete_notes' notes.id %}">
{% csrf_token %}
<button type="submit">Delete</button>
</form>
If you want to redirect to the task_detail
of a given task, you can add an extra parameter:
path('projects/checklist/delete_notes/<int:note_id>/<slug:slug>/', delete_notes, name='delete_notes'),
and redirect with:
from django.views.decorators.http import require_http_methods
@login_required
@require_http_methods(['POST', 'DELETE'])
def delete_notes(request, note_id, slug):
Note.objects.filter(id=note_id).delete()
return redirect('task_detail', slug=slug)
then in the form you should pass the slug of the task:
<form method="post" action="{% url 'delete_notes' notes.id task.slug %}">
{% csrf_token %}
<button type="submit">Delete</button>
</form>
Source:stackexchange.com