Skip to content

🔧 Split Settings into Development and Production Environments and updating the requirements #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

snipher-marube
Copy link
Contributor

Summary
This PR improves project configuration by splitting the Django settings into modular files for development and production environments. This structure follows best practices for environment-specific configuration and enhances maintainability, security, and developer experience.

Changes Introduced
📁 Created settings/base.py: common settings shared across all environments.

📁 Added settings/development.py: tailored for local development.

Enables DEBUG=True

Uses SQLite

Adds localhost to ALLOWED_HOSTS

📁 Added settings/production.py: ready for deployment

Sets DEBUG=False

Uses PostgreSQL with environment variables via python-decouple

Implements Django security best practices (HSTS, secure cookies, SSL redirects)

🔐 Improved use of decouple to load secrets and database config securely

🛡 Ensures ALLOWED_HOSTS is explicitly set in all environments

📦 Added .env.example to guide contributors on environment setup

Why This Is Needed
Separating settings avoids accidental use of development configurations in production.

Prepares the Codespace for real-world deployment scenarios.

Supports cleaner, more secure environment management.

How to Test
Copy .env.example to .env and populate required fields.

Run development server with:
python manage.py runserver --settings=hello_world.settings.development

To simulate production locally:
python manage.py runserver --settings=hello_world.settings.production

Notes

  • This update is fully backward-compatible with existing codespace environment variables like CODESPACE_NAME.

  • Environment-specific settings now prevent misconfigurations during deployment.

@Copilot Copilot AI review requested due to automatic review settings May 2, 2025 11:13
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the Django settings by splitting them into modular files for both development and production environments to enhance security, maintainability, and overall configuration management.

  • Updated manage.py to dynamically select between development and production settings based on the DEBUG flag.
  • Created dedicated settings files: base.py for common settings, development.py for local testing, and production.py for deployment.
  • Removed the legacy settings.py and updated the settings/init.py to conditionally import the correct configuration.

Reviewed Changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
manage.py Updated to dynamically set DJANGO_SETTINGS_MODULE based on DEBUG from the development file.
hello_world/settings/production.py New production settings with PostgreSQL and Django security best practices.
hello_world/settings/development.py New development settings using SQLite, with additional support for GitHub Codespaces.
hello_world/settings/base.py Common settings extracted for reuse in both production and development configurations.
hello_world/settings/init.py Imports settings based on DJANGO_SETTINGS_MODULE to ensure proper configuration load.
hello_world/settings.py Deprecated legacy settings file now removed.
Files not reviewed (1)
  • .env.example: Language not supported

Comment on lines +9 to +13
from hello_world.settings import development as settings
if settings.DEBUG:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_world.settings.development")
else:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_world.settings.production")
Copy link
Preview

Copilot AI May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the main() function imports the development settings first to check DEBUG, which may lead to confusion when switching to production. Consider using an environment variable or another configuration flag to determine the settings module prior to importing any settings.

Suggested change
from hello_world.settings import development as settings
if settings.DEBUG:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_world.settings.development")
else:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_world.settings.production")
settings_module = os.getenv("DJANGO_SETTINGS_MODULE", "hello_world.settings.development")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant