skip to Main Content

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

2

Answers


  1. ====== 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)
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search