Concern or Doubt
why this add in project url
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
This code snippet is checking if the DEBUG setting in Django is set to True. If it is, then it is adding two URL patterns to the urlpatterns list:
- One for serving static files defined in the STATIC_URL setting, using the static function from django.conf.urls.static, with the document_root argument set to the STATIC_ROOT setting.
- One for serving media files defined in the MEDIA_URL setting, using the same static function with the document_root argument set to the MEDIA_ROOT setting.
It's worth noting that serving static files with Django in production is not recommended due to performance reasons. Instead, it is recommended to use a separate web server like Nginx or Apache to serve static files. Therefore, this code snippet should only be used during development.
I hope this helps!
why this add in project settings
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] STATIC_ROOT = os.path.join(BASE_DIR, 'assets') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
The issue is likely that Heroku serves static files differently than your local server. You should configure Django to handle serving static files in production, either by using a CDN or the WhiteNoise library. In your specific case, it seems that you are not using WhiteNoise, which is a very common way of serving static files in production when using Heroku.
To configure WhiteNoise, you can do the following:
Install WhiteNoise by running pip install whitenoise.
Add whitenoise.middleware.WhiteNoiseMiddleware to your MIDDLEWARE setting, as shown below:
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # Add this line 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
/staticfiles/
python manage.py collectstatic
I hope this helps!
No comments