Home > database >  Media files showing 404 debug is false using Django development server
Media files showing 404 debug is false using Django development server

Time:10-02

I have been trying to test my Django project before deploying it on a cpanel

settings.py

STATIC_URL = '/static/'

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]

CodePudding user response:

====== in project 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
from django.urls import path

urlpatterns = [
    path("", include("myapp.urls")),
    path('admin/', admin.site.urls),

]

urlpatterns  = static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

CodePudding user response:

urlpatterns modification to serve static files is recommended in development.

If you want to serve your static files from the same server that’s already serving your site, the process may look something like:

  1. Push your code up to the deployment server.
  2. On the server, run collectstatic to copy all the static files into STATIC_ROOT.
  3. Configure your web server to serve the files in STATIC_ROOT under the URL STATIC_URL. For example, here’s how to do this with Apache and mod_wsgi.

How to use Django with Apache and mod_wsgi

  • Related