[Django]-Django Framework: Object does not display on web page for specific pk

5👍

I don’t understand why you’re doing datum = data._meta.get_fields() in the view. That gets the class-level field objects, which you don’t want at all. Don’t do that; pass the object directly to the template.

You will also need to remove the loop in the template and pass the object directly as item.

As a further optimisation, you can use get_object_or_404 rather than catching the DoesNotExist exception and raising a 404 manually.

def adherence_agreement(request,id):
    item = get_object_or_404(UserInfo, pk=id)
    return render(request, 'adherence_agreement.html', {'item': item})

1👍

Your template is expecting an iterable and you’re retrieving a single item so you can place it in a list to make it iterable. You can also cut down on code by using get_object_or_404 (which throws a 404 if not found).

def adherence_agreement(request, id):
    data = get_object_or_404(UserInfo, pk=id)
    return render(
        request,
        'adherence_agreement.html', 
        {
            'items': [data]
        }
    )

Leave a comment