Accessing MEDIA_ROOT in templates, always blank/missing

332 views
Skip to first unread message

Stodge

unread,
Mar 22, 2012, 8:11:45 AM3/22/12
to Django users
I'm trying to reference MEDIA_ROOT in my template but it's blank. I
have MEDIA_ROOT defined in settings.py:

MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)),
'static')

I have the correct context processor:

TEMPLATE_CONTEXT_PROCESSOR = (
'django.core.context_processors.i18n',
'django.core.context_processors.auth',
'django.core.context_processors.media',
)

In my template:

MEDIA_ROOT: {{MEDIA_ROOT}}

But all I see is:

MEDIA_ROOT:

So MEDIA_ROOT is blank or doesn't exist. Did I miss something? This is
Django 1.2.3, thanks. Oh and I realise there is a newer version of
Django out there but I can't upgrade my app... yet..

Thanks again

Stodge

unread,
Mar 22, 2012, 8:21:13 AM3/22/12
to Django users
Oh that's surprising; the media context processor only creates
MEDIA_URL.

kenneth gonsalves

unread,
Mar 22, 2012, 8:22:34 AM3/22/12
to django...@googlegroups.com
On Thu, 2012-03-22 at 05:11 -0700, Stodge wrote:
> In my template:
>
> MEDIA_ROOT: {{MEDIA_ROOT}}
>
> But all I see is:
>
> MEDIA_ROOT:
>
> So MEDIA_ROOT is blank or doesn't exist. Did I miss something?

https://docs.djangoproject.com/en/1.0/ref/templates/api/#django-core-context-processors-media

you need MEDIA_URL
--
regards
Kenneth Gonsalves

Stodge

unread,
Mar 22, 2012, 8:25:34 AM3/22/12
to Django users
No I need MEDIA_ROOT, I want to experiment with:

<script src="{{ MEDIA_ROOT }}/extern/js/jquery.js"></script>

Thanks

On Mar 22, 7:22 am, kenneth gonsalves <law...@thenilgiris.com> wrote:
> On Thu, 2012-03-22 at 05:11 -0700, Stodge wrote:
> > In my template:
>
> > MEDIA_ROOT: {{MEDIA_ROOT}}
>
> > But all I see is:
>
> > MEDIA_ROOT:
>
> > So MEDIA_ROOT is blank or doesn't exist. Did I miss something?
>
> https://docs.djangoproject.com/en/1.0/ref/templates/api/#django-core-...

Davinir F Campos Jr

unread,
Mar 22, 2012, 8:28:43 AM3/22/12
to django...@googlegroups.com
Stoge, you DO need MEDIA_URL in turn to build the URL in which script src is based on.

MEDIA_ROOT is the (complete) physical path to the file. Do you want to expose your filesystem?

[ ]s
Davinir

2012/3/22 Stodge <sto...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Tom Evans

unread,
Mar 22, 2012, 8:30:30 AM3/22/12
to django...@googlegroups.com
On Thu, Mar 22, 2012 at 12:25 PM, Stodge <sto...@gmail.com> wrote:
> No I need MEDIA_ROOT, I want to experiment with:
>
> <script src="{{ MEDIA_ROOT }}/extern/js/jquery.js"></script>
>
> Thanks
>

MEDIA_ROOT is a file-system location, not a URL-space location, and
should not be exposed in templates. It is nonsense to use media root
as a prefix to the URL-space location of your javascript files.

Cheers

Tom

Stodge

unread,
Mar 22, 2012, 8:33:14 AM3/22/12
to Django users
All good points thanks. I'm only experimenting and learning so this
won't hit production. Point taken though. :)

kenneth gonsalves

unread,
Mar 22, 2012, 8:35:13 AM3/22/12
to django...@googlegroups.com
On Thu, 2012-03-22 at 05:25 -0700, Stodge wrote:
> No I need MEDIA_ROOT, I want to experiment with:
>
> <script src="{{ MEDIA_ROOT }}/extern/js/jquery.js"></script>
>
> Thanks

in the view:
from django.conf import settings

and send settings.MEDIA_ROOT to the template

or build a templatetag to do the same thing.
--
regards
Kenneth Gonsalves

Reply all
Reply to author
Forward
0 new messages