Cancel Button on Delete View not returning to correct page

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

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

<a href="{% url 'trips:trip-detail'  %}"

now the 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:

 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

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

Hi @addwebsolution and thanks for replying,

Models Below

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(
    last_modified = models.DateTimeField(auto_now=True)
    date = models.DateField(
    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(
    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)

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

    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 = "/"
        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.

1 Like