Page not found (404) No Booking matches the given query - Django web application

I am having trouble figuring out where my issue is. I am parsing a slug to the url, via my view pulling from the SlugField in my model. For an object instance that exists in my database, the slug is being parsed successfully into the url. However, I am receiving the above error and cannot work out why.

The corresponding model is Booking, and the slug field is as follows:

booking_reference = models.SlugField(verbose_name="Slug Field", blank=False, unique=True)

(I wanted to use the booking_reference as the slug field).

My is as follows:

class EditBookingView(UpdateView, NextUrlMixin):
    model = Booking
    form_class = BookingForm
    template_name = 'bookings/edit_booking.html'
    success_url = '/'
    default_next = '/'

    def form_valid(self, form):
        form.instance.user = self.request.user
        next_path = self.get_next_url()
        return redirect(next_path)

    def get_context_data(self, **kwargs):
        context = super(EditBookingView, self).get_context_data(**kwargs)
        context['slug'] = self.kwargs['booking_reference']
        return context

    def get_object(self):
        slug = self.kwargs.get('booking_reference')
        return get_object_or_404(Booking, booking_reference=slug)

And my are:

from django.urls import path
from .views import (

app_name = 'bookings'
urlpatterns = [
    path('createbooking/<str:slug>/', CreateBookingView.as_view(), name='create_booking'),
    path('viewbookings/<str:slug>/', ViewBookingsView.as_view(), name='view_booking'), 
    path('editbooking/<str:slug>/', EditBookingView.as_view(), name='edit_booking'),
    path('cancelbooking/<str:slug>/', DeleteBookingView.as_view(), name='delete_booking'),

Please note, I have a list view for which the booking reference is being displayed under each model instance successfully. It is from this view that I can then go into the above EditView with the following line in the listview template:

<div class="row text-center mt-3">
    <a class="btn btn-primary justify-content-md-center mb-5" href='{% url "bookings:edit_booking" slug=item.booking_reference %}' role="button" >Edit Booking</a>

As I say I have double checked whether the slug being parsed into my url is in fact present in my database under the "booking_reference" field for the model instance, and it is.

Any help would be greatly appreciated!

To trace the problem back:

You are getting a 404

You have a get_object_or_404 in your get_object() method, where you look up a Booking based on a variable 'slug'

The variable slug is set when you ask for self.kwargs.get('booking_reference')

And the kwarg 'booking_reference' is set...?

In your URLs file the variable is <str:slug>. Django doesn't actually know much else about it in relation to the model until it is used in some way,eg, to get an instance.

So trying to get('booking_reference') is returning None. The model knows what a booking_reference is, but the URL doesn't provide that connection. Hence the 404.

To fix: you just need to switch to


(You may also need to update your get_context() in a similar manner)

Back to Top