Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django image not found

Tags:

python

django

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/'
like image 981
znawca Avatar asked Sep 20 '25 02:09

znawca


1 Answers

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

like image 145
Kamlesh Avatar answered Sep 22 '25 16:09

Kamlesh