# Cancel Button on Delete View not returning to correct page

**URL:** <https://forum.djangoproject.com/t/cancel-button-on-delete-view-not-returning-to-correct-page/23534>\
**Category:** Forms & APIs\
**Created:** [August 31, 2023, 9:36am UTC](https://forum.djangoproject.com/t/cancel-button-on-delete-view-not-returning-to-correct-page/23534 "2023-08-31T09:36:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonphob](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jonphob/32/15710_2.png) [@jonphob](https://forum.djangoproject.com/u/jonphob)\
**Post date:** [August 31, 2023, 9:36am UTC](https://forum.djangoproject.com/t/cancel-button-on-delete-view-not-returning-to-correct-page/23534/1 "2023-08-31T09:36:06Z")

</div>

Just learning Django and have come up against a blocker I’m having trouble resolving.

I am creating an app where you can add a ‘trip’, and to these trips you can add a ‘note’

I have managed to get adding,updating and deleting of trips working , but I cannot get the deletion of notes to work correctly, specifically if i want to cancel the deletion on the note\_confirm\_delete template.

This is my DeleteView

```auto
class NoteDeleteView(LoginRequiredMixin, DeleteView):
    model = Note
    pk_url_kwarg = 'note_pk'

    def get_success_url(self, **kwargs):
        success_url = f'/trips/{str(self.kwargs["trip_pk"])}'
        return success_url

```

and this is the href link in my template

```auto
<a href="{% url 'trips:trip-detail' object.id %}"

```

now the object.id is the id of the note but i need to get the pk of the related trip as on deletion I want to go back to trips/int:pk

How can i do this, I’ve messed around with context etc but getting a bit tied up in knots, can anyone push me in right direction to help me solve please?

Relevant URLs paths are below:

```auto
 path('trips/<int:pk>/', TripDetailView.as_view(), name='trip-detail'),
    # Page to add new trips
    path('trips/new/', TripCreateView.as_view(), name='trip-create'),
    # Path to update trip
    path('trips/<int:pk>/update/', TripUpdateView.as_view(), name='trip-update'),
    # Path to delete trip
    path('trips/<int:pk>/delete/', TripDeleteView.as_view(), name='trip-delete'),
    # Path to add new note
    path('trips/<int:pk>/note/new/', NoteCreateView.as_view(), name='note-create'),
    # Path to update note
    path('trips/<int:trip_pk>/note/<int:note_pk>/update/', NoteUpdateView.as_view(), name='note-update'),
    # Path to delete note
    path('trips/<int:trip_pk>/note/<int:note_pk>/delete/', NoteDeleteView.as_view(), name='note-delete'),

```

I have another query re the way I have constructed the note url paths, as in is this best way to do it? But i will ask another question, however unsure if its related to my issue in this question

Thanks in advance

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/addwebsolution/32/23762_2.png) [@addwebsolution](https://forum.djangoproject.com/u/addwebsolution)\
**Post date:** [August 31, 2023, 10:31am UTC](https://forum.djangoproject.com/t/cancel-button-on-delete-view-not-returning-to-correct-page/23534/2 "2023-08-31T10:31:23Z")

</div>

Do yo have trip model and note model with trip foreign key in it?

---

<div class="post-metadata">

**Author:** ![jonphob](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jonphob/32/15710_2.png) [@jonphob](https://forum.djangoproject.com/u/jonphob)\
**Post date:** [August 31, 2023, 10:37am UTC](https://forum.djangoproject.com/t/cancel-button-on-delete-view-not-returning-to-correct-page/23534/3 "2023-08-31T10:37:34Z")

</div>

Hi @addwebsolution and thanks for replying,

Models Below

```auto
class Note(models.Model):
    name = models.CharField(max_length=50)
    trip = models.ForeignKey(Trip, on_delete=models.CASCADE)
    category = models.ForeignKey(NoteCategoriesDefault, on_delete=models.CASCADE)
    date_added = models.DateTimeField(default=timezone.now)
    last_modified = models.DateTimeField(auto_now=True)
    date = models.DateField(default=timezone.now)
    content = models.TextField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)

class Trip(models.Model):
    title = models.CharField(max_length=100)
    date_added = models.DateTimeField(default=timezone.now)
    last_modified = models.DateTimeField(auto_now=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    start_date = models.DateField()
    end_date = models.DateField(null=True)
    image = models.ImageField(default='default.png', upload_to='trip_header_pics')
    description = models.TextField(null=True)

```

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/addwebsolution/32/23762_2.png) [@addwebsolution](https://forum.djangoproject.com/u/addwebsolution)\
**Post date:** [August 31, 2023, 11:00am UTC](https://forum.djangoproject.com/t/cancel-button-on-delete-view-not-returning-to-correct-page/23534/4 "2023-08-31T11:00:42Z")

</div>

> [@jonphob](#):
>
> ```auto
> def get_success_url(self, **kwargs):
> success_url = f'/trips/{str(self.kwargs["trip_pk"])}'
> return success_url
> 
> ```

Instead of overriding this ^^^ method, you can override this

```auto
    def delete(self, request, *args, **kwargs):
        """
        Call the delete() method on the fetched object and then redirect to the
        success URL.
        """
        self.object = self.get_object()
        success_url = "/"
        self.object.delete()
        return HttpResponseRedirect(success_url)

```

and from self.object you will get instance of your note model and from that instance you can retrieve trip object and pk as well.
