Django not recognizing the MEDIA_URL path?

前端 未结 8 1837
鱼传尺愫
鱼传尺愫 2021-02-03 13:23

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

相关标签:
8条回答
  • 2021-02-03 13:47

    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

    0 讨论(0)
  • 2021-02-03 13:48

    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
    
    0 讨论(0)
  • 2021-02-03 13:50

    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.

    0 讨论(0)
  • 2021-02-03 13:57

    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

    0 讨论(0)
  • 2021-02-03 14:02

    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

    0 讨论(0)
  • 2021-02-03 14:03

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

    0 讨论(0)
提交回复
热议问题