0

I want to run always in debug=false in django. However, it works only when i turned to debug to true. Otherwise not. In setting.py

     DEBUG = False

also, in setting.py

    STATIC_URL = '/static/'
    STATIC_ROOT = '/static/'
    MEDIA_URL = '/media/'   
    if DEBUG:
        STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
    else:
        STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')   
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

In main urls.py

    from django.conf import settings
    from django.conf.urls.static import static  
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('',include('mysite.urls')),
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I have directory of media, static, staticfiles in the directory where manage py is located.

If i put code exactly in apache will my code works. P.S In development also i want to fix debug as false always.

  • Django is not supposed to serve media or static files in production https://docs.djangoproject.com/en/4.2/howto/static-files/ In DEBUG=False mode it is expected that media and static files dont work. – Ivan Starostin Jun 14 '23 at 11:19
  • Does this answer your question? [Django media files not showing with Debug = False on production - Django 1.10](https://stackoverflow.com/questions/44555187/django-media-files-not-showing-with-debug-false-on-production-django-1-10) – Ivan Starostin Jun 14 '23 at 11:20
  • Configure Apache / Nginx for serving these files in prod – Ivan Starostin Jun 14 '23 at 11:21

1 Answers1

0

Apache and Static/Media Files

Are you running everything via Apache and mod_wsgi? If so, make sure to set your Apache conf files to handle the 'static' alias appropriately, and in so doing you should find that your setup works once you've run python manage.py collectstatic. (Example, assuming you host your project inside /var/www/myproject/, is below):

Alias /static /var/www/myproject/static
    <Directory /var/www/myproject/static>
            Require all granted
    </Directory>

You may need to do likewise for 'media' if your image is in your media directory.


Development Server and Static/Media Files

If you are still running the development server (i.e. python manage.py runserver) and you are doing so with DEBUG = False, then you will encounter issues serving static files and media files.

However, you can circumnavigate these issues by running python manage.py runserver --insecure, with the massive caveat that this is not ideal and expressly the opposite of a stable production set up - Django is not meant to serve these files in production.


Final tip:

If you are going to this trouble because you want a development server and a production server with different DEBUG values (i.e. True in development, False in production), an easy fix is to use the django-environ package plus a .env file, which you exclude from version control, and then set your DEBUG value inside your .env files. You'll then have a consistent settings.py file capable of returning DEBUG=True in development and DEBUG=False in production.

The link to this package is here: https://django-environ.readthedocs.io/en/latest/