I’m trying to have a modal popup asking for confirmation before deleting the object using DeleteView. So far, I’ve tried solutions from this thread using the following code.
views.py
class SessionCancelView(
SuccessMessageMixin, LoginRequiredMixin, UserPassesTestMixin, DeleteView
):
model = Session
success_url = "/profile"
success_message = "Session was cancelled successfully"
# FIXME: success message not showing - eh maybe not necessary tho
def test_func(self):
session = self.get_object()
if self.request.user == session.student:
return True
return False
def get(self, request, *args, **kwargs):
return self.post(request, *args, **kwargs)
profile.html
<!-- Modal -->
<div class="modal fade" id="sessionDeleteModal" tabindex="-1" role="dialog" aria-labelledby="sessionDeleteModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Cancel Session</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to cancel this session?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button class="btn btn-outline-danger" type="submit">Yes, Cancel</button>
<form method="POST" action="{% url 'session-cancel' session.id %}">
{% csrf_token %}<input type="submit" value="DELETE">
</form>
</div>
</div>
</div>
</div>
.
.
.
{% for session in user_sessions %}
{% if session.is_upcoming %}
<div class="card bg-light mb-3" style="width: 18rem;">
<div class="card-header">{{ session.date|date:"F d [l]" }}</div>
<div class="card-body">
<h5 class="card-title">{{ session.get_timeblock_display }}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{ session.weekday }}</h6>
<p class="card-text">{{ session.helptype }}</p>
<a class='btn btn-outline-secondary btn-small mt-1 mb-1' href="{% url 'session-edit' session.id %}">Edit</a>
<a class='btn btn-outline-danger btn-small mt-1 mb-1'
href="{% url 'session-cancel' session.id %}">Cancel</a>
<button type="button" class="btn btn-outline-danger btn-small mt-1 mb-1" data-toggle="modal"
data-target="#sessionDeleteModal">
Modal Cancel
</button>
</div>
</div>
{% endif %}
{% endfor %}
urls.py
from django.urls import path
from . import views
from .views import (
SessionListView,
SessionDetailView,
SessionCreateView,
SessionEditView,
SessionCancelView,
)
urlpatterns = [
path("", views.home, name="scheduler-home"),
path("about/", views.about, name="scheduler-about"),
path("sessions/", SessionListView.as_view(), name="scheduler-sessions"),
path("sessions/<int:pk>/", SessionDetailView.as_view(), name="session-detail"),
path("sessions/new/", SessionCreateView.as_view(), name="session-create"),
path("sessions/<int:pk>/edit", SessionEditView.as_view(), name="session-edit"),
path(
"sessions/<int:pk>/cancel", SessionCancelView.as_view(), name="session-cancel"
),
path("issues/", views.report_issues, name="scheduler-issues"),
]
However, I’m getting the following error when loading profile.html page:
Exception Type: NoReverseMatch at /profile/
Exception Value: Reverse for 'session-cancel' with arguments '('',)' not found. 1 pattern(s) tried: ['sessions/(?P<pk>[0-9]+)/cancel$']
I am assuming this is happening because modal has no way of having session.id
passed in, hence causing a NoReverseMatch error. Would appreciate if anyone could help point out what I did wrong!