0

I know this is a prevalent topic, but I still have yet to find a recent answer that helps. I have setup my dev environment to serve MEDIA in what I believe is the correct way, but images are still not resolving. Paths being return propery in template, but I believe the issue is with URLS.PY? This is my settings.py:

if DEBUG:
    #For local
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    if DEV:
        MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
        MEDIA_URL = '/media/'
        ADMIN_MEDIA_PREFIX = 'http:/127.0.0.1:8000/admin-media/'

This is the template itself (attached image is the rendered template itself, which seems correct):

<div><img src="{{MEDIA_URL}}products/{{product.filename}}"></div>

This is my primary URLS.py (top level, not application specific, but I've added this line in the app specific one as well):

urlpatterns = [
    url(r'^', include('drivel.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^tinymce/', include('tinymce.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

What am I missing? I am down to the wire and can't seem to get this going. I also am open to recos for the production site as I'm new to Django and best practices in production.

Environment is Py 2.7, Django 1.8.

enter image description here

  • What's the value of ``BASE_DIR``? – Grimmy Apr 30 '17 at 12:42
  • BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) – GeranTabee Apr 30 '17 at 12:52
  • Printing this out from my settings.py file when exec run server returns:/Users/gerantabee/Desktop/Webtest/doublerainbow/doublerainbow which is the accurate base path to my where settings lives – GeranTabee Apr 30 '17 at 12:54
  • Since the `media` folder lives under `/Users/gerantabee/Desktop/Webtest/doublerainbow/` then the `MEDIA_ROOT` should be matching this path. Change `file` with `__file__` inside the `abspath`, please. – nik_m Apr 30 '17 at 13:20
  • The comments field is stripping out the underscores because I omitted markdown but it is in fact `__file__` – GeranTabee Apr 30 '17 at 13:24
  • Note, i'm having this identical issue in production, which is puzzling given i'm not using run server and this shouldn't apply. Do I still need to create a url config for media files being served directly from the server in production? – GeranTabee Apr 30 '17 at 13:28
  • `doublerainbow ├── doublerainbow ├── drdotcom │   ├── media │   ├── static ├── manage.py └── media └── products` – GeranTabee Apr 30 '17 at 13:46
  • Sorry it's not taking the structure properly into the comment. That's why included the screenshot in my previous comment: [IMG]http://i36.photobucket.com/albums/e32/fred17/Screen%20Shot%202017-04-30%20at%209.38.19%20AM_zpsbk4byphq.png[/IMG] – GeranTabee Apr 30 '17 at 13:48
  • And which `media` dir is the one that actually contains the files? – nik_m Apr 30 '17 at 13:53
  • The top level one. Oddly the files uploaded were split across both. I think I changed my settings.py halfway through and it split the files halfway through. Moving all files to the top level one seems to have resolved the issue, for now. My bigger issue will be going to production. It seems like I may need a URL rewrite to serve media since the directory lives outside of the app directory. – GeranTabee Apr 30 '17 at 13:57
  • This is gonna be tough... Show your models please. – nik_m Apr 30 '17 at 14:01
  • It's now working on both, strangely enough. I think consolidating all media to the top level directory fixed the issue. Thanks again for your help, you rock. – GeranTabee Apr 30 '17 at 14:11
  • Wow! That's strange and great too. I didn't do anything, though! – nik_m Apr 30 '17 at 14:12

0 Answers0