Files
pack/examples/python-django/mysite/settings.py
T
868fcdabc4 test: add hiredis python test (#349)
* feat: add Redis with Hiredis support to python-django example

Add Redis caching alongside existing PostgreSQL database connection to
demonstrate multi-service integration. Configure Django to use Redis
cache backend with Hiredis parser for improved performance.

Changes:
- Add Redis service to docker-compose.yml
- Add redis and hiredis dependencies to pyproject.toml
- Configure Redis cache in Django settings with HiredisParser
- Create home view demonstrating both Redis cache and direct connection
- Update test.json to include REDIS_URL environment variable

* chore: update Python bytecode cache for django example

* chore: remove Python cache files and update gitignore

Remove __pycache__ files that were incorrectly committed and add Python-specific patterns to .gitignore to prevent future commits of cache files.

Changes:
- Remove all __pycache__/*.pyc files from git tracking
- Add Python cache patterns to .gitignore (__pycache__/, *.pyc, *.pyo, etc.)
- Update CLAUDE.md to explicitly document that cache files should never be committed

The .gitignore was missing Python patterns because it was generated from a Go template and never updated for Python examples.

* docs: clarify guidance on build artifacts in gitignore

* chore: update uv.lock for python-django with redis dependencies

* fix: remove incorrect HiredisParser configuration

Redis-py automatically uses hiredis when it's installed - no explicit
parser_class configuration is needed. The incorrect configuration was
causing ImportError since HiredisParser is not exposed as a public API.

Hiredis will be automatically detected and used by redis-py for
improved performance when the hiredis package is available.

---------

Co-authored-by: Hanzo Dev <[email protected]>
2025-11-18 16:46:50 -07:00

151 lines
3.8 KiB
Python

"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 5.1.6.
For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""
from pathlib import Path
import os
from urllib.parse import urlparse
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-pky33&-@65dl3@+tf$f^%6n=+d+soh$#dx&3tps67ccgjlt1!m'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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',
]
ROOT_URLCONF = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
WSGI_APPLICATION = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
def get_db_config():
database_url = os.environ.get('DATABASE_URL')
url = urlparse(database_url)
return {
'ENGINE': 'django.db.backends.postgresql',
'NAME': url.path[1:],
'USER': url.username,
'PASSWORD': url.password,
'HOST': url.hostname,
'PORT': url.port,
}
DATABASES = {
'default': get_db_config()
}
def get_redis_config():
redis_url = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')
return {
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
'LOCATION': redis_url,
}
CACHES = {
'default': get_redis_config()
}
# Password validation
# https://docs.djangoproject.com/en/5.1/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.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'