4

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/'
1
  • Are you using the development server or a web server? Did you enable static and media serving? Please post your urls.py so that we can check.
    – Selcuk
    Feb 15, 2016 at 4:05

2 Answers 2

4

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

0

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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.