有人能理解为什么我在这里得到无反向匹配错误吗:
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"),
]
到目前为止,它只发生在我的edit/delete
路径(带有<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
文件:
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)
相应的Html为:
<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>
错误消息:
NoReverseMatch: Reverse for 'delete_reservation' with no arguments not found. 1 pattern(s) tried: ['reservations/delete/(?P<pk>[-a-zA-Z0-9_]+)/$']
真的希望这是一件简单的事情。感谢您的帮助。如果您需要查看更多代码,请告诉我,我会根据需要进行更新。
如果Django看到您显式设置了Field.primary_key
,它不会添加自动id
列
所以您应该使用pk
或reservation_id
而且pk
更独立于实际的主键字段,即您不需要关心主键字段是称为id
还是reservation_id
或其他什么。
<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>
或使用pk:
<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>