简体   繁体   中英

Django not recognizing the MEDIA_URL path?

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',
)

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

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))

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/ .

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

Please read the official Django DOC carefully and you will find the most fit answer.

The best and easist way to solve this is like below.

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The related url is: https://docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user

Thanks a lot for your help everyone. I was able to solve it as

settings.py -->

MEDIA_ROOT = '/home/patrick/Documents/code/projects/test/media/'
MEDIA_URL = 'http://localhost:8000/media/'

urls.py -->

(r'^media/(?P<path>,*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),

index.html -->

img src="/media/images/logo.jpg"

Again, thanks a lot, I was going crazy trying to find out how to do all of this. This solution worked for me on a Django version 1.2.5 Development server running Ubuntu 10.04.

I had a similar problem, I have a one-page-app and Apparently I had a newbie mistake that made django miss my media path i had this:

url(r'^', home, name="home"),

instead of this:

url(r'^$', home, name="home"),

the missing $ made my url pattern miss my media url

I was also having the same problem, this is how I got it done.

If you are using a FileField in your model class instead of MEDIA_URL use .url member to access the url of your file. For eg: something like this

href = "{{ some_object.file_name.url }}" 

here file_name is the FileField in model class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM