Django视图重定向NoReverseMatch错误

时间:2019-05-01 07:08:50

标签: python django

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

models.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})

urls.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

2 个答案:

答案 0 :(得分:0)

尝试

from django.urls import reverse

redirect(reverse('personnel_detail', kwargs={'pk': doc.ID_Personnel}))

答案 1 :(得分:0)

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

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

但从根本上讲,这表明您的字段名不正确。就像我说的那样,这是对对象的引用,而不是ID,因此它不应具有暗示它是ID的名称。只需将其命名为Personnel。 (此外,Python风格是使用lower_case_with_underscore作为属性,因此实际上应该只是personnel。)