Can anyone figure out why I'm getting a no reverse match error here:
urls.py
:
urlpatterns = [
path('', views.ReservationsFormView.as_view(), name='reservations'),
path('edit/<slug:pk>/', EditReservationView.as_view(), name="edit_reservation"),
path('reservation_complete/', ReservationCompleteView.as_view(),
name="reservation_complete"),
path('reservations_account/', ReservationAccountView.as_view(),
name="reservations_account"),
path('delete/<slug:pk>/', DeleteReservationView.as_view(),
name="delete_reservation"),
]
It's only happening, so far, on my edit/delete
paths (the one's with the <slug:pk>
)
views.py
:
class ReservationsFormView(CreateView):
model = Reservations
template_name = "reservations/reservations.html"
form_class = ReservationForm
success_url = "reservation_complete/"
def form_valid(self, form):
form.instance.user = self.request.user
return super().form_valid(form)
class EditReservationView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
form_class = ReservationForm
template_name = 'reservations/edit_reservation.html'
success_url = "/reservations_account/"
model = Reservations
def test_func(self):
return self.request.user == self.get_object().user
class ReservationCompleteView(CreateView):
template_name = "reservations/reservation_complete.html"
success_url = "/reservation_complete/"
form_class = ReservationForm
model = Reservations
class ReservationAccountView(ListView):
template_name = "reservations/reservations_account.html"
model = Reservations
def get_context_data(self, **kwargs):
context = {
'reservations': self.model.objects.filter(user=self.request.user),
}
return context
class DeleteReservationView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
""" A view to delete an reservation """
model = Reservations
success_url = "/reservations_account/"
def test_func(self):
return self.request.user == self.get_object().user
Models.py
File:
class Reservations(models.Model):
""" reservation form categories and attributes """
user = models.OneToOneField(User, on_delete=models.CASCADE,
unique=True)
name = models.CharField(max_length=50)
phone_number = models.CharField(validators=[phoneNumberRegex],
max_length=16, unique=True)
email = models.EmailField()
date = models.DateField()
time = models.CharField(choices=time_options, default="12pm",
max_length=10)
number_of_party = models.IntegerField(choices=party_size,
default=1)
reservation_id = models.UUIDField(primary_key=True,
default=uuid.uuid4, editable=False)
Corresponding Html is:
<button href="{% url 'edit_reservation' reservation.id %}" type="button" class="btn btn-dark">Edit</a>
<button href="{% url 'delete_reservation' reservation.id %}" type="button" class="btn btn-dark">Delete</a>
Error Message:
NoReverseMatch: Reverse for 'delete_reservation' with no arguments not found. 1 pattern(s) tried: ['reservations/delete/(?P<pk>[-a-zA-Z0-9_]+)/$']
Really hoping it's something simple. Any help is appreciated. If you need to see more code, let me know and i'll update with what's needeed.
If Django sees you’ve explicitly set Field.primary_key
, it won’t add the automatic id
column.
So you should use pk
or reservation_id
.
also pk
is more independent from the actual primary key field i.e. you don't need to care whether the primary key field is called id
or reservation_id
or whatever.
<button href="{% url 'edit_reservation' reservation.reservation_id %}" type="button" class="btn btn-dark">Edit</a>
<button href="{% url 'delete_reservation' reservation.reservation_id %}" type="button" class="btn btn-dark">Delete</a>
<button href="{% url 'edit_reservation' reservation.pk %}" type="button" class="btn btn-dark">Edit</a>
<button href="{% url 'delete_reservation' reservation.pk %}" type="button" class="btn btn-dark">Delete</a>