Skip to content

Commit

Permalink
CRUD - delete
Browse files Browse the repository at this point in the history
  • Loading branch information
heloisatambara committed May 17, 2023
1 parent 07877bc commit 68ce46f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
5 changes: 4 additions & 1 deletion books/templates/index.html
Expand Up @@ -11,7 +11,10 @@

<ul>
{% for book in books %}
<li> {{ book.id }} - {{ book.title }} by {{ book.author }} <a href="{% url 'edit' book.id %}">edit</a></li>
<li> {{ book.id }} - {{ book.title }} by {{ book.author }}
<a href="{% url 'edit' book.id %}">edit</a>
<a href="{% url 'delete' book.id %}">delete</a>
</li>
{% endfor %}
</ul>

Expand Down
3 changes: 2 additions & 1 deletion books/urls.py
@@ -1,10 +1,11 @@
from django.contrib import admin
from django.urls import path, include
from .views import home, save, edit, update
from .views import home, save, edit, update, delete

urlpatterns = [
path('', home),
path('save/', save, name="save"),
path('edit/<int:id>', edit, name="edit"),
path('update/<int:id>', update, name="update"),
path('delete/<int:id>', delete, name="delete"),
]
5 changes: 5 additions & 0 deletions books/views.py
Expand Up @@ -22,4 +22,9 @@ def update(request, id):
book.title = request.POST.get("title")
book.author = request.POST.get("author")
book.save()
return redirect(home)

def delete(request, id):
book = Book.objects.get(id=id)
book.delete()
return redirect(home)

0 comments on commit 68ce46f

Please sign in to comment.