page not found django.views.static.serve
Asked Answered
P

1

6

first post so be gentle.

I am trying to display a static image in django. I can upload the image to my media directory but when I try to display it I get a nothing. If I copy the url to the browser I get ...

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/media/Will_and_Matt.jpg Raised by: django.views.static.serve

detail.py

...
<img src="{{ student.photo.url }}" class="img-responsive">
...

model.py

class Student(models.Model):
    photo = models.FileField( blank=True, null=True)

project urls.py

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


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^opencmis/', include('opencmis.urls')),
]

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

settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

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

My update form works as it created the media folder and places images in it.

The URL pattern seems to match /media as it doesn't give an error there it just doesn't display the image.

Any ideas?

Polycrates answered 13/8, 2016 at 7:44 Comment(2)
document_root=settings.STATIC_URL should be document_root=settings.STATIC_ROOT and similar for MEDIA_ROOT. You want the filesystem path in there, not the URL.Humboldt
You Sir, are a gent. This worked perfectly!Polycrates
P
7

document_root=settings.STATIC_URL should be document_root=settings.STATIC_ROOT and similar for MEDIA_ROOT. You want the filesystem path in there, not the URL.

– dhke

Polycrates answered 15/8, 2016 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.