1 Django Tips
Josh Gavant редактировал(а) эту страницу 2019-03-28 11:42:44 -07:00

Oryx attempts to build and run Django apps appropriately, but some configuration is not automated, so following are instructions for setting this yourself. If you'd like us to include these automatically please open an issue, thanks!

You may also want to review Heroku's django-heroku package for ideas.

  1. Add App Service's default suffix to the list of allowed hosts in settings.py:

    // settings.py
    ALLOWED_HOSTS = [
        '.azurewebsites.net'
    ]
    
  2. Oryx runs manage.py collectstatic on your behalf unless you specify the DISABLE_COLLECTSTATIC env var (see our [configuration doc](. But make sure you've set STATIC_ROOT in settings.py:

    // settings.py
    STATIC_ROOT = './static/'
    
  3. To host static files in your web app, add the whitenoise package to requirements.txt and the configuration for it to settings.py.

    # requirements.txt
    whitenoise==4.1.2
    
    // settings.py
    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'whitenoise.middleware.WhiteNoiseMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'corsheaders.middleware.CorsMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]
    
    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
    
  4. To apply database schema migrations following a build, create a post-build shell script and set its repo-relative path in the POST_BUILD_SCRIPT_PATH environment variable, e.g. POST_BUILD_SCRIPT_PATH=./scripts/post.sh. The script should contain the following.

    #! /usr/bin/env sh
    
    python manage.py migrate