Breaking News

New Updates

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.
This is a common way of serving static and media files during development, where the DEBUG setting is usually set to True. In production, serving static and media files is usually done using a web server like Nginx, Apache, or CDN services like Amazon S3.

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',
]
Add STATIC_ROOT to your settings file, as shown below:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Add a reference to STATIC_ROOT in your .gitignore file (if it doesn't exist already), to prevent it from being committed to your repository.
/staticfiles/
Collect all static files into the static files directory by running the following command:
python manage.py collectstatic
This will collect all the static files in your project into the STATIC_ROOT directory, which can then be served by WhiteNoise.

I hope this helps!

No comments