Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

django delete post noReverseMatch

Trying to delete a post, but somehow getting a NoReverseMatch

views.py

@login_required
def task_detail(request, slug):
    '''
    Detailed view of all tasks on given project
    '''
    context = {}
    checklist   = get_object_or_404(Checklist, slug=slug)
    context.update({'checklist':checklist})
    form        = NotesForm(request.POST or None)
    if request.method == "POST":
        if form.is_valid():
            print("\n\n for is valid")
            author = Profile.objects.get(user=request.user) 
            new_note = form.save(commit=False)
            new_note.user = author
            new_note.checklist = checklist
            new_note.save()

            return redirect('task_detail', slug=slug)

    context.update({
        'form': form,
        'title': checklist,
    })

    return render(request, 'projects/checklist.html', context)

@login_required
def delete_notes(request, note_id = None):
    del_note = Note.objects.get(id = note_id)
    del_note.delete()

    return redirect('teams')

My urls.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

urlpatterns = [
    path('projects/', teams, name='teams'),
    path('projects/project/<slug>/', projects, name='projects'),
    path('projects/tasks/<slug>/', project_detail, name='project_detail'),
    path('projects/checklist/<slug>/', task_detail, name='task_detail'),
    path('projects/checklist/delete_notes/', delete_notes, name='delete_notes'),
]

in the html I have just a href with the url to delete

<a class="dropdown-item text-danger" href="{% url 'delete_notes' notes.id %}">Delete</a>

Getting; Reverse for ‘delete_notes’ with arguments ‘(14,)’ not found. 1 pattern(s) tried: [‘projects/checklist/delete_notes/\Z’]

Not really sure what is missing, thought the slug from checklist was already passed?

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading