views:

643

answers:

5

So I'm trying to get TinyMCE up and running on a simple view function, but the path to the tiny_mce.js file gets screwed up. The file is located at /Users/home/Django/tinyMCE/media/js/tiny_mce/tiny_mce.js. I believe that /media/js/tiny_mce/tiny_mce.js is correct, as the development server has no access to files above the root of the Django project folder. In the rendered page, it says in the debugger that the javascript file was not found. This is because it was trying to look through /js/tiny_mce/tiny_mce.js without addressing the /media/ part of the pathname.

Anyway, here's the script snippet for the javascript in a template named 'simple.html'. <script type="text/javascript" src="{{ MEDIA_URL }}/js/tiny_mce/tiny_mce.js"></script> <script type="text/javascript"> tinyMCE.init({ mode : "textareas", theme : "simple" }); </script>

And this is what the vital parts of my settings is like.

MEDIA_ROOT = os.path.join(_base, 'media')
MEDIA_URL = 'http://127.0.0.1:8000/media/'
ADMIN_MEDIA_PREFIX = '/media/'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'tinymce',
)
+2  A: 

Do you have the media context processor in your settings?

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.media',
)

You might also try putting the following in your settings to see if the debug messages reveal anything:

TEMPLATE_DEBUG = True
ars
I do have the debug mode on, but there are no errors. All it is is that the javascript file doesn't load.
RaDeuX
Now that I look closely, even with the pathname being corrected, it still doesn't load.<script src="http://127.0.0.1:8000/media/js/tiny_mce/tiny_mce.js" type="text/javascript"> 1Page not found: /media/js/tiny_mce/tiny_mce.js
RaDeuX
Try view source on the page after setting TEMPLATE_DEBUG=True .. the debug message might be within the html head/script section where your media_url is referenced.
ars
Do you have a URL for /media? This page might help: http://docs.djangoproject.com/en/dev/howto/static-files/
ars
Do I need a URL for media? Can't I access the javascript from a view function that accesses the javascript file?
RaDeuX
Not sure what you mean by "accesses". Your view function is simply rendering the template -- so it returns the HTML to the browser. The browser parses the HTML and makes another call to find related files like the script URL. Do you have a separate view to render that URL (js file)? If not, then use the link I provided to django docs, which explains how to statically serve that file in your local/dev environment. Presumably your production server handles this by having an entry to resolve /media files statically.
ars
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}), is what I put for media files. I also can't access anything in the media folder index, including the folder itself, as it gives me a permission denied page.
RaDeuX
Can you confirm that your settings.MEDIA_ROOT is correct? Try printing it and check the path. Also, permission denied is security related, so make sure you have read permissions on the filesystem for that directory.
ars
Yes, MEDIA_ROOT appears to be correct. The interpreter gives me this:>>> print settings.MEDIA_ROOT/Users/home/Django/tinyMCE/mediaThe read permissions are all there. Perhaps it needs the execute permission as well.
RaDeuX
Now that I look at it more carefully, the /media folder is readable to everyone.
RaDeuX
I found the issue. The ADMIN_MEDIA_PREFIX was set as /media/, so I changed it to /media/admin/. David Wolever was right. Thank you very much for your help!
RaDeuX
+5  A: 

You can either pass it to the template manually as others have suggested or ensure that you are using a RequestContext instead of plain Context. RequestContext will automatically populate the context with certain variables including MEDIA_URL and other media-related ones.

Example from docs:

def some_view(request):
    # ...
    return render_to_response('my_template.html',
                              my_data_dictionary,
                              context_instance=RequestContext(request))
Ben
This solved the issue with the pathname. However, the javascript isn't loading properly, so I must be doing something else wrong as well.
RaDeuX
+3  A: 

It looks like ars has answered your real question… But you'll run into another problem: MEDIA_URL must be different from ADMIN_MEDIA_PREFIX. If they aren't, the ADMIN_MEDIA_PREFIX will take precedence. I usually fix this by changing ADMIN_MEDIA_PREFIX to /admin-media/.

David Wolever
Hm, didn't know that. It's not part of my issue, but I'll keep that in mind if I ever hit a huge wall like this again.
RaDeuX
It turns out that you were right. Even though my MEDIA_URL was http://localhost:8000/media/, it was essentially still the same folder. So not only does the pathname must be different but the folder that it's referring to must be different as well. Thanks for your help!
RaDeuX
+1  A: 

It looks like your are using the debug server (your url is http://127.0.0.1:8000/...) . Did you install the static serve in your urls.py?

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),
)

The 'show_indexes':True options make possible to browse your media. Go to your medias root http://127.0.0.1:8000/media/ and see it there is something

luc
Should I use http://localhost:8000/ instead then? Even with the static serve, the debugger still says the javascript file is missing.
RaDeuX
This seems the most logical. @RaDeuX - can you load any static files?
czarchaic
I can't. I keep getting a "Page not found" error.
RaDeuX
Does http://127.0.0.1:8000/media/ show something?
luc
A: 

whomever suggested to change the media admin variable in settings saved me some time, thank you

Mac