Issue
I am trying to implement soft delete in django but getting the following error:
NoReverseMatch at /admin/trip-issue-report/
Reverse for 'soft-delete-trip-issue' with arguments '(3,)' not found. 1 pattern(s) tried: ['admin/soft\-delete\-trip\-issue/$']
My code:
models.py
class TripIssue(models.Model):
trip = models.ForeignKey(Trip, on_delete=models.CASCADE)
issue = models.ForeignKey(Issue, on_delete=models.CASCADE)
isSolved = models.BooleanField(blank=False, default=False)
is_deleted = models.BooleanField(default=False)
deleted_at = models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def soft_delete(self):
self.is_deleted = True
self.deleted_at = timezone.now()
self.save()
views.py
class TripIssueSoftDelete(DeleteView):
model = TripIssue
success_url = reverse_lazy('trip-issue-report')
template_name = 'trip_issue.html'
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.soft_delete()
return HttpResponseRedirect(self.get_success_url())
urls.py
path('soft-delete-trip-issue/', views.TripIssueSoftDelete, name="soft-delete-trip-issue"),
trip_issue.html template
{% for trip_issue in trip_issues %}
<tr>
<td>{{trip_issue.trip}}</td>
<td>{{trip_issue.issue}}</td>
<td>{{trip_issue.isSolved}}</td>
<td>{{trip_issue.updated_at}}</td>
<td><a href="{% url 'dashboard:soft-delete-trip-issue' trip_issue.id %}"><i class="fas fa-minus-circle"></i></a></td>
</tr>
{% endfor %}
</tbody>
So I need your help to fix the issue and implement soft delete successfully.
Thanks in advance.
Happy Coding :)
Solution
Here is the solution I've done:
urls.py
updated the url < str:pk > and viewset [as this is class based view] with .as_view()
path('soft-delete-trip-issue/<str:pk>/', views.TripIssueSoftDelete.as_view(), name="soft-delete-trip-issue"),
Extra Tips: if you get this error __init__() takes 1 positional argument but 2 were given
make sure your decorator in view is working or try to fix that.
Happy Coding :)
Answered By - Mehady
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.