0

I am getting Page not found (404) error after I set up the media folder as follows in settings.py:

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

then in urls.py:

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


urlpatterns = [
    path('admin/', admin.site.urls),
]

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

After this when I try to run the server, I am getting the following message: Using the URLconf defined in personal_portfolio.URLs, Django tried these URL patterns, in this order:

admin/
^media/(?P<path>.*)$
The empty path didn’t match any of these.

Where is the mistake?

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
corey95
  • 1
  • 1
  • 1
    Is this happening when you visit the main domain (somehting like 127.0.0.1:8000) , if so it doesn't seem like you have a url pattern for that, so django doesn't know how to serve that page. Read the first part of the [official tutorial](https://docs.djangoproject.com/en/4.0/intro/tutorial01/) and see if you can see how you could fix that. If you're still struggling let us know. – Scb May 25 '22 at 20:07
  • If I delete urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) and MEDIA_URL = '/media/' MEDIA_ROOT = BASE_DIR / 'media', the main domain is working properly. – corey95 May 25 '22 at 20:22
  • you're probably seeing the default index page (with the rocket) which confirms that the instalation was successful. If you read the message it says that it's only there because it's in debug mode and you haven't added any URLs. As soon as you change the defaults by adding the media one, it complains because of the reason I mentioned above. You need to route the url for the empty path `path('', some_view)`. I would suggest you go over the tutorial before trying to build an app of your own. You shouldn't be worrying about media if you don't even have basic views and routing figured out – Scb May 25 '22 at 21:08

0 Answers0