![](/img/trans.png)
[英]Django - reverse_lazy in DeleteView yields NoReverseMatch
[英]reverse lazy error NoReverseMatch at django DeleteView
删除 1 项分析后,我试图返回到患者分析列表。 但无法取得适当的成功 url
所以这是我的 model:
class PatientAnalysis(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
analysis_date = models.DateTimeField(help_text = "Разделяйте даты точками! Используйте '/' или '-'")
# analysis_type = models.IntegerField(choices = ANALYSIS_CHOICES) #перевести в таблицу
analysis_type = models.ForeignKey(AnalysisType, on_delete=models.CASCADE, default=1)
analysis_data = models.DecimalField(max_digits=5, decimal_places=2)
def __str__(self):
return f"{self.patient}"
def get_analysis_type(self):
return f"{self.analysis_type}"
def get_absolute_url(self):
return reverse('journal:patient_analysis', kwargs={'hist_num_slug':self.patient.pk})
class Meta:
unique_together = ('analysis_date','analysis_type',)
这是列出每位患者的所有分析的 class
class PatientAnalysisListView(ListView):
model = PatientAnalysis
template_name = 'journal/patient_analysis.html'
context_object_name = 'analysis'
def get_context_data(self, *, object_list=None, **kwargs):
<...>
return context
def get_queryset(self):
return PatientAnalysis.objects.filter(patient__hist_num=self.kwargs['hist_num_slug']).order_by('-analysis_date')
在这里我坚持使用下一个代码:
class PatientAnalysisDeleteView(DeleteView):
# Form --> Confirm Delete Button
# model_confirm_delete.html
model = PatientAnalysis
success_url = reverse_lazy('journal:patient_analysis', kwargs={'key': model.patient})
并收到错误:
NoReverseMatch at /journal/patientanalysis_delete/49
Reverse for 'patient_analysis' with keyword arguments '{'key': <django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor object at 0x7fb88b8c0d90>}' not found. 1 pattern(s) tried: ['journal/patient_analysis/(?P<hist_num_slug>[-a-zA-Z0-9_]+)\\Z']
Request Method: POST
Request URL: http://127.0.0.1:8000/journal/patientanalysis_delete/49
Django Version: 4.1.3
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'patient_analysis' with keyword arguments '{'key': <django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor object at 0x7fb88b8c0d90>}' not found. 1 pattern(s) tried: ['journal/patient_analysis/(?P<hist_num_slug>[-a-zA-Z0-9_]+)\\Z']
Exception Location: /home/verts/.local/lib/python3.10/site-packages/django/urls/resolvers.py, line 828, in _reverse_with_prefix
Raised during: journal.views.PatientAnalysisDeleteView
Python Executable: /usr/bin/python3
Python Version: 3.10.6
Python Path:
['/home/verts/Documents/social-network/social_network/medicine',
'/usr/lib/python310.zip',
'/usr/lib/python3.10',
'/usr/lib/python3.10/lib-dynload',
'/home/verts/.local/lib/python3.10/site-packages',
'/usr/local/lib/python3.10/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Sat, 26 Nov 2022 21:24:52 +0300`
试图使功能获得成功 url 但未成功。
编辑:除了 Willem Van Onsem 解决方案之外,您还可以通过 html 代码简单地反转:
<form action="../patient_analysis/{{patientanalysis.patient.hist_num}}" method="POST">
您可以覆盖.get_success_url()
方法[Django-doc]以返回我们重定向到的路径:
from django.urls import reverse
class PatientAnalysisDeleteView(DeleteView):
model = PatientAnalysis
def get_success_url(self):
return reverse(
'journal:patient_analysis',
kwargs={'hist_num_slug': self.object.patient_id},
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.