Rene Chan
Rene Chan

Reputation: 985

Using UUID in View_Detail page in Django

I have an app that is working fine, but i just changed the ID to UUIDm and the route no longer work. I have the following route:

path("documents/uuid:uuid/",views.document_show, name="document_show"),

and the following view:

def document_show(request, id):
    student_list = DocumentAttachment.objects.all()
    for student in student_list:
        print("we are printing the list of imaged uploaded :", student.attachment)
    context = {}
    try:
        context["data"] = Document.objects.get(id=id)
    except Document.DoesNotExist:
        raise Http404('Book does not exist')
    return render(request, "documents/show.html", context)

With the architecture: template/documents/show.html

May I ask what is the right way to setup the routes please?

Upvotes: 0

Views: 258

Answers (1)

Abdul Aziz Barkat
Abdul Aziz Barkat

Reputation: 21802

Firstly as pointed out by @Boma Anjang in their comment you missed the <> symbols in your pattern which then should be:

path("documents/<uuid:uuid>/",views.document_show, name="document_show"),

Next your view is defined as document_show(request, id) this needs to be updated as the captured arguments are passed as keyword arguments to the view and hence need to have the correct name.

Similarly Document.objects.get(id=id) also needs to be updated.

Hence your view should be something like:

def document_show(request, uuid):
    student_list = DocumentAttachment.objects.all()
    for student in student_list:
        print("we are printing the list of imaged uploaded :", student.attachment)
    context = {}
    try:
        context["data"] = Document.objects.get(uuid=uuid) # Not sure of the name of your field for the uuid, edit as per the name
    except Document.DoesNotExist:
        raise Http404('Book does not exist')
    return render(request, "documents/show.html", context)

Upvotes: 2

Related Questions