没有反向匹配。想法如何解决?
No reverse match. Ideas how to fix?
谁能弄清楚为什么我在此处收到 无反向匹配 错误:
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>
谁能弄清楚为什么我在此处收到 无反向匹配 错误:
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>