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:
- Push your code up to the deployment server.
- On the server, run collectstatic to copy all the static files into
STATIC_ROOT
. - Configure your web server to serve the files in
STATIC_ROOT
under the URLSTATIC_URL
. For example, here’s how to do this with Apache andmod_wsgi
.