0

I am unable to display images uploaded by the admin user through admin on a template.

settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

url.py

from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin

import web.views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', web.views.home, name='home')
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

page.html

<img src="{{ event.image.url }}" alt="{{ event.title }}" />

also tried

<img src="{% get_media_prefix %}{{ event.image.url }}" alt="{{ event.title }}" />

I am not sure what I am missing. Thanks for your help.

--------------UPDATE--------------

django project structure

Chirdeep Tomar
  • 4,281
  • 8
  • 37
  • 66
  • What is the output of `{{ event.image.url }}`? Does it show the relative path you would expect? – Adam Hopkins Feb 18 '17 at 19:53
  • /media/images/events/old.jpg is what I get. Yes the path is correct. – Chirdeep Tomar Feb 18 '17 at 19:58
  • 1
    let me know about your three path project... and where your project is running now? deployed at server or under local development? My previous project is similiar with your case that deployed under server.. one reason is because permissions of path project, also the files. and fixes with changed that permissions such as `chown -R www-data:www-data /path/to/yourproject`, `644` for the files, or other else.. – binpy Feb 18 '17 at 20:45
  • I just think, if your configuration is look good, maybe you having a problem that related with [this question](http://stackoverflow.com/questions/21797372/django-errno-13-permission-denied-var-www-media-animals-user-uploads). As a side note, if you check at the browser a full path of your image, an example: `http://127.0.0.1:8000/media/images/events/old.jpg` and having permission denied. – binpy Feb 18 '17 at 20:49
  • I am on a windows machine, permissions shouldn't be a problem. – Chirdeep Tomar Feb 18 '17 at 20:52
  • @ChirdeepTomar please update your question and let me know about your tree path project.. – binpy Feb 18 '17 at 20:54
  • @SancaKembang updated. Please have a look. – Chirdeep Tomar Feb 20 '17 at 19:14
  • if you change `import web.views` to `from web.views import home` and then change `web.views.home` to `home`, what happend? – binpy Feb 20 '17 at 21:44
  • No change, still cant load. I didn't think it would make any difference. – Chirdeep Tomar Feb 21 '17 at 05:32
  • In the terminal django is showing 200 OK for image calls. – Chirdeep Tomar Feb 21 '17 at 09:44

1 Answers1

-1

I had change the home path to remove the regex which was causing the routing to match it before the media url.

    url(r'', web.views.home, name='home')
Chirdeep Tomar
  • 4,281
  • 8
  • 37
  • 66