ara-foundation / bike-rental-vietnam

Web service for renting motorbikes and services around it
0 stars 1 forks source link

static for test #30

Open AntonGyaltsen opened 2 months ago

AntonGyaltsen commented 2 months ago

The issue you're encountering with Django's admin panel static files not rendering on Heroku while having DEBUG=True often relates to how static files are handled in production environments like Heroku. Here’s a step-by-step approach to resolve the problem:

When DEBUG=False, Django will not serve static files by default, so you need to ensure proper static file handling.

2. Set ALLOWED_HOSTS:

Make sure you have set the ALLOWED_HOSTS in your settings.py file, which should include your Heroku app's domain and other hosts you intend to use:

ALLOWED_HOSTS = ['*']

3. Use whitenoise for Static Files:

Heroku doesn’t serve static files automatically, so you need to configure a middleware to handle this. whitenoise is a popular package used to serve static files in production.

# settings.py

MIDDLEWARE = [
    # Other middlewares...
    'whitenoise.middleware.WhiteNoiseMiddleware',  # Add Whitenoise middleware here
    # ...
]

# Static files settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')  # Ensure STATIC_ROOT is defined for production

# Whitenoise settings for serving static files efficiently
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

In main urls.py — Иначе не будет работать при Debug = True

if settings.DEBUG:
    import debug_toolbar

    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += (path("__debug__/", include(debug_toolbar.urls)),)
mikiminimouse commented 2 months ago

extra actions for seting up static for test:

AntonGyaltsen commented 2 months ago

extra actions for seting up static for test:

  • pip install django-debug-toolbar -in setings.py Add debug_toolbar.middleware.DebugToolbarMiddleware to MIDDLEWARE.

Dubug toolbar is not necessary. I forgot to exclude it from instruction. It is mostly for testing sql-queries (which based on queries through Django ORM in view functions) for optimisation.