One more thing to deploy a Django project for public access

Configure Django project for public access
For Django project to work in the web browser, some additional changes may be needed.
Open the settings.py file for the Django project (inside the PROJECT_ROOT\config or PROJECT_ROOT\PROJECT_NAME folder) and follow these steps:

  1. Add this code to the top of the file.
    import os
  2. Disable debug mode.
    DEBUG = False
  3. Set ALLOWED_HOSTS for Django project remotely accessible.
    • Public access to Django project:
      ALLOWED_HOSTS = ['*']
    • or…

    • Restricted access, e.g. only requests from the 11.22.33.44 IP address:
      ALLOWED_HOSTS = ['11.22.33.44']
  4. Set STATIC_URL and STATIC_ROOT to serve static files.
    STATIC_URL = 'static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'config/static')
  5. *The config folder is the folder that contains settings.py, urls.py, wsgi.py, … inside.

  6. Build static files by executing the following command.
    C:\Users\USER_NAME\Bitnami Django Stack projects\PROJECT_ROOT>python manage.py collectstatic --noinput
  7. This command will copy static files in folder C:\Bitnami\djangostack-3.1.4-0\apps\django\Django-3.1.4-py3.7.egg\django\contrib\admin\static to folder C:\Users\USER_NAME\Bitnami Django Stack projects\PROJECT_ROOT\config\static
    We have to change something in the C:\Users\USER_NAME\Bitnami Django Stack projects\
    PROJECT_ROOT\conf\httpd-app.conf, like this…
    #Alias /PROJECT_ROOT/static "C:/Bitnami/djangostack-3.1.4-0/apps/django/
    Django-3.1.4-py3.7.egg/django/contrib/admin/static"
    Alias /mfdw_site/static "C:/Users/USER_NAME/Bitnami Django Stack projects/
    PROJECT_ROOT/config/static"
    I suppose …. this means that we will use the static files those are inside the folder C:\Users\USER_NAME\Bitnami Django Stack projects\PROJECT_ROOT\config\static.
    Ref: Serving Static Files in Python With Django, AWS S3 and WhiteNoise

  8. Additional correcting the TIME_ZONE in settings.py file for the Django project
    (inside the PROJECT_ROOT\config or PROJECT_ROOT\PROJECT_NAME folder)
    #TIME_ZONE = 'UTC'
    TIME_ZONE = 'Asia/Bangkok'
  9. Ref: List_of_tz_database_time_zones

Leave a Reply

Your email address will not be published. Required fields are marked *