Monday 15 March 2021

How do I reference a local media file in a Django template

I have set up my media root, media url etc. so the file will show up when I call

http://127.0.0.1:8000/media/something/somewhere/my_pdf.pdf

Now I want to show the same file within a template (i.e. the PDF in an object or embed tag plus some other stuff. I pass the path to the file (i.e. everything after 'media' in the view context, something like:

def test_doc(request):`
    p = r'something/somewhere/my_pdf.pdf'`
    return render(request, 'documents/test.html', {'pdf':p})

So what do I have to put in my template? I tried

<object data="">something</object>

and many many variations thereof. What am I doing wrong?



from How do I reference a local media file in a Django template

No comments:

Post a Comment