gpt4 book ai didi

python - Django View 重定向NoReverseMatch错误

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:17 25 4
gpt4 key购买 nike

在 Django 项目中,表单保存后出现错误 NoReverseMatch,重定向到“父 View ”

模型.py

这是一个模型代码 - 人员

class Personnel(models.Model):
ID_Personnel = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
NameFirst = models.CharField(max_length=50)
NameLast = models.CharField(max_length=50)
IsActive = models.BooleanField(default=True, editable=True)
TimeCreation = models.DateTimeField(default=datetime.now, editable=False)

class IdentityDocument(models.Model):
ID_IdentityDocument = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
ID_Personnel = models.ForeignKey(Personnel, on_delete=models.PROTECT)
NumberSerial = models.CharField(max_length=50)
DateIssue = models.DateField(null=True)
DateExpiration = models.DateField(null=True)
NameIssuer = models.CharField(max_length=100, null=True)
IsActive = models.BooleanField(default=True, editable=True)
TimeCreation = models.DateTimeField(default=datetime.now, editable=False)

views.py - 人员

def personnel_edit(request, pk):
person = get_object_or_404(Personnel, ID_Personnel=pk)
if request.method == 'POST':
form = PersonnelForm(request.POST, instance=person)
if form.is_valid():
person = form.save(commit=False)
person.save()
return redirect('personnel_detail', pk=person.ID_Personnel)
else:
form = PersonnelForm(instance=person)
return render(request, 'personnel/personnel_edit.html', {'form': form})

def personnel_detail(request, pk):
person = get_object_or_404(Personnel, ID_Personnel=pk)
try:
document = IdentityDocument.objects.filter(ID_Personnel=pk).order_by('ID_IdType', '-DateExpiration').distinct(
'ID_IdType')
except IdentityDocument.DoesNotExist:
document = None
return render(request, 'personnel/personnel_detail.html',
{'person': person, 'document': document})

views.py - 在表单中设置 ID_Personnel

def identitydocument_new(request, fkey):
if request.method == 'POST':
form = IdentityDocumentForm(request.POST)
if form.is_valid():
iddoc = form.save(commit=False)
iddoc.ID_Personnel = Personnel.objects.get(ID_Personnel=fkey)
iddoc.save()
return redirect('personnel_detail', pk=fkey)
else:
form = IdentityDocumentForm()
return render(request, 'personnel/identitydocument_edit.html', {'form': form})
def identitydocument_edit(request, pk):
doc = get_object_or_404(IdentityDocument, ID_IdentityDocument=pk)
if request.method == 'POST':
form = IdentityDocumentForm(request.POST, instance=doc)
if form.is_valid():
doc = form.save(commit=False)
doc.save()
return redirect('personnel_detail', pk=doc.ID_Personnel)
else:
form = IdentityDocumentForm(instance=doc)
return render(request, 'personnel/identitydocument_edit.html', {'form': form})

url.py

from django.urls import path
from . import views

urlpatterns = [
path('', views.personnel_list, name='personnel_list'),
path('personnel/<uuid:pk>/', views.personnel_detail, name='personnel_detail'),
path('personnel/new', views.personnel_new, name='personnel_new'),
path('personnel/<uuid:pk>/edit/', views.personnel_edit, name='personnel_edit'),
path('personnel/identitydoc/<uuid:fkey>/', views.identitydocument_new, name='identitydocument_new'),
path('personnel/identitydoc/<uuid:pk>/edit/', views.identitydocument_edit, name='identitydocument_edit'),
]

Form创建新文档并保存正常,但编辑后出现NoReverseMatch错误。

Traceback

最佳答案

正如该错误所示,ID_Personnel 是对整个相关人事文档的引用。 URL 需要该对象的 ID。所以你应该这样做:

return redirect('personnel_detail', pk=doc.ID_Personnel_id)

但从根本上来说,这表明您的字段名称不正确。正如我所说,这是对对象的引用,而不是 ID,因此它的名称不应暗示它是 ID。只需将其命名为人员即可。 (另外,Python 风格是使用 lower_case_with_underscore 作为属性,所以实际上它应该只是 personnel。)

关于python - Django View 重定向NoReverseMatch错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55932741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com