znawca
znawca

Reputation: 279

Django image not found

i would like to paste media image into template but i see this:

this image

When i click on it to show image i see:

Page not found
" C:\Users\troll\myprojects\proj\album\media\images/photos/hour.jpg " doesn't exist

I see in this address there is something with slashes, some are normal slashes and some are forward slashes - but how to change it?

my settings.py file:

MEDIA_ROOT = 'C:/Users/troll/myprojects/proj/album/media/'

MEDIA_URL = '/media/'

Upvotes: 4

Views: 4106

Answers (2)

Frank G
Frank G

Reputation: 49

In my case I had to edit my urls.py;

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


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("main.urls")),
    path('', include('django.contrib.auth.urls'))
]

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

and my media root looked like;

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

Upvotes: 0

Kamlesh
Kamlesh

Reputation: 2056

You should post your settings.py and urls.py for clear understanding, which version of django are you using?

still i am considering Django==1.9+

your settings.py

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

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
# Media files
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media/'

main urls.py

urlpatterns = [
    url(r'^', admin.site.urls),

]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # just after your urlspatterns

Create staticfiles directory and run command

python manage.py collectstatic

and make sure in your templates(form) you have wrote this enctype="multipart/form-data".

<form action="{% url 'index' %}" method="post" enctype="multipart/form-data">

try to debug (print) your path of image

Hope this will help

Upvotes: 4

Related Questions