Django Deploy on Render.com: Page isn't redirecting properly browser error

I'm trying to deploy a Django application using Docker to https://render.com. The Docker container runs successfully, but when I try to open the website in a browser (Firefox Developers Edition), I get this error: Error in Firefox

I've also tried opening the application in various other browsers, but I'm also getting similar errors. During the development on my machine I don't receive any errors.

Dockerfile:


FROM python:3.12

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    DJANGO_SETTINGS_MODULE=config.settings.prod

# Set the working directory
WORKDIR /app

RUN python --version

# Install Node.js and npm
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs

# Install dependencies for MariaDB
RUN apt-get update && \
    apt-get install -y python3-dev default-libmysqlclient-dev build-essential pkg-config

# Install Poetry
RUN pip install poetry

# Copy pyproject.toml and poetry.lock
COPY pyproject.toml poetry.lock /app/

# Configure Poetry to not use virtualenvs
RUN poetry config virtualenvs.create false

# Install Python dependencies
RUN poetry install --no-dev --no-root

# Copy the entire project
COPY . /app/

# Install Tailwind CSS (requires Node.js and npm)
RUN python manage.py tailwind install --no-input;

# Build Tailwind CSS
RUN python manage.py tailwind build --no-input;

# Collect static files
RUN python manage.py collectstatic --no-input;

# Migrate the database
RUN python manage.py migrate --no-input;

# Expose port 8000
EXPOSE 8000

# Start the application with Gunicorn
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "config.wsgi:application"]

My production settings are split into two files (base.py & prod.py). I'm using django.db.backends.mysql as database engine.

base.py:

from pathlib import Path
import os
from dotenv import load_dotenv

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent

# Load environment variables from .env file
load_dotenv(BASE_DIR / ".env")

# SECRET_KEY specifies the secret key that Django will use to sign its cookies
SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY")

# ALLOWED_HOSTS specifies the list of IP addresses or domain names that are allowed to visit the Django application
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(",")

# WSGI_APPLICATION specifies the WSGI application that Django will use to serve the application
WSGI_APPLICATION = 'config.wsgi.application'

# INSTALLED_APPS specifies the list of applications that Django will load when it starts up
INSTALLED_APPS = [
    'home',
    'contact',
    'blogs',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tailwind',
    'theme',
    'django_browser_reload',
    "django_htmx",
]

# MIDDLEWARE specifies the list of middleware classes that Django will use to process requests
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
    "django_browser_reload.middleware.BrowserReloadMiddleware",
    "django_htmx.middleware.HtmxMiddleware",
]

# INTERNAL_IPS specifies the IP addresses that are allowed to visit the Django debug toolbar
INTERNAL_IPS = [
    "127.0.0.1",
]

# ROOT_URLCONF specifies the root URL configuration that Django will use to resolve URLs
ROOT_URLCONF = 'config.urls'

# TEMPLATES specifies the list of directories where Django will look for templates
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR / 'templates',
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# STATIC_ROOT specifies the directory where Django will store static files
STATICFILES_DIRS = [
    BASE_DIR / "static/",
]

# STATIC_URL specifies the URL that will be used to access static files
STATIC_URL = 'static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True

# DEFAULT_AUTO_FIELD specifies the type of primary key that will be used for all Django models
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# TARGET_APP_NAME is the name of the app that you want to target for the theme
TAILWIND_APP_NAME = 'theme'

# NPM_BIN_PATH specifies the path to the npm binary
NPM_BIN_PATH = "/usr/bin/npm"

prod.py:

from config.settings.base import *

DEBUG = False

SECURE_HSTS_SECONDS = 31536000
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True

# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': os.environ.get("PROD_DATABASE_ENGINE"),
        'NAME': os.environ.get("PROD_DATABASE_NAME"),
        'USER': os.environ.get("PROD_DATABASE_USER"),
        'PASSWORD': os.environ.get("PROD_DATABASE_PASSWORD"),
        'HOST': os.environ.get("PROD_DATABASE_HOST"),
        'PORT': os.environ.get("PROD_DATABASE_PORT"),
    }
}

Tail of Deployment Logs:


==> Deploying...
[2024-08-21 11:25:44 +0000] [1] [INFO] Starting gunicorn 23.0.0
[2024-08-21 11:25:44 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2024-08-21 11:25:44 +0000] [1] [INFO] Using worker: sync
[2024-08-21 11:25:44 +0000] [7] [INFO] Booting worker with pid: 7
==> Your service is live 🎉```

I already googled and asked ChatGPT and Claude, but didn't get an answer.

Back to Top