0

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?

4
  • path("documents/<uuid:uuid1>/",views.document_show, name="document_show") You forget to give < > sign Commented May 4, 2021 at 14:23
  • this one does : ValidationError at /documents/38d3459c-d5dc-40ac-91ba-3e43e4a41b09/ ['“<built-in function id>” is not a valid UUID.'] Commented May 4, 2021 at 14:24
  • You are not getting an error like TypeError: document_show() got an unexpected keyword argument 'uuid'? did you update your view function as document_show(request, uuid)? If you did likely you forgot to update this line Document.objects.get(id=id) Commented May 4, 2021 at 14:37
  • And it works! thank you very much! Can make an answer and I add a point for you ? Commented May 4, 2021 at 14:38

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Abdul, last question please, do you know how to make a link to a a detail_view under the document_show function please ?
@FrankyDoul Don't know your url patterns, etc. but in the template one generally uses the url template tag and in the view one uses reverse function or reverse_lazy (in case the urls might not be loaded yet)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.