0
votes

I am running on a local server and when I go to http://127.0.0.1:8000/media/ in my browser it says page not found.

Settings:

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

Root URL:

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'^polls/', include('mysite.polls.urls')),
     url(r'^admin/', admin.site.urls),
     url(r'^submit/', include('mysite.uploads.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

Model for User Upload:

from django.db import models
class Document(models.Model):
    description = models.CharField(max_length=255, blank=True)
    document = models.FileField(upload_to='documents/')
    uploaded_at = models.DateTimeField(auto_now_add=True)

Following the Django documentation:

Serving files uploaded by a user during development

During development, you can serve user-uploaded media files from MEDIA_ROOT using the django.contrib.staticfiles.views.serve() view.

This is not suitable for production use! For some common deployment strategies, see Deploying static files.

For example, if your MEDIA_URL is defined as /media/, you can do this by adding the following snippet to your 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)

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

1
A 404 for http://127.0.0.1:8000/media/ is not a problem. You need to try to view a media file, e.g. http://127.0.0.1:8000/media/image.jpg.Alasdair

1 Answers

0
votes

As pointed above in the comments by Alasdair, This is the normal behaviour of Django. If you visit the full file path like 127.0.0.1/media/file.jpg, Django will render the file instead of raising a 404 error.

Why is this happening?

If you look at the source of the view that serves the media/static files, you'll find these lines:

if os.path.isdir(fullpath):
    if show_indexes:
        return directory_index(newpath, fullpath)
    raise Http404(_("Directory indexes are not allowed here."))

What it does is, it checks if the path requested is a directory or not. If yes, then it checks if the variable show_indexes is True, then it will return the index of the files inside the media directory. Since, show_indexes is False by default, it raises 404.

To set show_indexes to True, you can do this:

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

This won't raise the 404 and will display a list of files inside media dir.


P.S.: I don't think displaying file indices is a good idea, unless, of course, that's something you really want. It puts server under extra load to generate the indices. And someone can download the files recursively using a program like wget etc. Even those files that are meant to be private.