59

I am able to upload the files to media folder( '/peaceroot/www/media/') that I have set up in settings.py as below

MEDIA_ROOT = '/peaceroot/www/media/'
MEDIA_URL = '/media/'

But through admin I tried to access the uploaded image file

http://localhost:8000/media/items/1a39246c-4160-4cb2-a842-12a1ffd72b3b.jpg

then I am getting 404 error.

The file exists at peaceroot/www/media/items/1a39246c-4160-4cb2-a842-12a1ffd72b3b.jpg

Akshat Zala
  • 710
  • 1
  • 8
  • 23
madhu131313
  • 7,003
  • 7
  • 40
  • 53

6 Answers6

118

Add media url entry in your project urlpatterns:

from django.conf.urls.static import static
from django.conf import settings

...
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
v1k45
  • 8,070
  • 2
  • 30
  • 38
  • 10
    Also Make sure you add the `static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)` in the `urls.py` file in the main project app and not in the other installed apps. In my case I was doing this wrong, so faced the problem. – Mohith7548 Jun 26 '19 at 17:04
  • 3
    Link to the documentation: [Serving files uploaded by a user during development](https://docs.djangoproject.com/en/3.0/howto/static-files/#serving-uploaded-files-in-development). Note: "works only in debug mode" and "not suitable for production use!". – Benoit Blanchon Jun 19 '20 at 13:01
35

The better way for MEDIA_ROOT is,

try to make media path dynamic will be easy when you shift your project.

Settings.py

BASE_DIR = os.path.dirname(os.path.dirname(__file__))


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

urls.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Look at this

https://docs.djangoproject.com/en/dev/howto/static-files/

Artyom Tsoy
  • 2,238
  • 1
  • 13
  • 18
Kamlesh
  • 2,032
  • 2
  • 19
  • 35
6

Just to add: in case the other answers do not work for you, try putting the static url before the other ones. Like so:

urlpatterns = static(...) + [...]

What may be happening is that some of your patterns in the list prevent the request from reaching the static handlers. So putting the static handlers first solves this. Worked for me.

Seyi Shoboyejo
  • 489
  • 4
  • 11
2

This is a server error. I'm assuming you are using Nginx. Just add this in your Nginx Configuration file(/etc/nginx/sites-available/example.com) just under location /static/

location /media/ {
    root /home/user/myprojectdir;
}

Here, user should be your username you created and myprojectdir should be your project directory.

1

In template create link by anchor tag and add .url in the end to that fileobject e.g

{% for post in post %}

   <a href="{{post.imagefilename.url}}" > 

{% endfor %}
star700p
  • 21
  • 3
0

In my development server I fixed it by commenting out these lines in settings.py

STATICFILES_DIRS = (
     os.path.join(BASE_DIR, 'static'),
)
Sumithran
  • 6,217
  • 4
  • 40
  • 54