Skip to content

Commit b21c80a

Browse files
committed
Change default connection to database to use host different than docker container
- After setting up the virtual environment for the project (note missing instruction how to work locally with venv in `Readme.md`) and after running the `makemigrations --dry-run` or starting the webserver, application is trying to connect with hardcoded hostname `db` used from Dockerfile - This patch allows to add multiple hostname from environment variable separed by delimiter (I liked `,`) and since the create type in settings.py is `list` type it is free to use first value from a list. - Proof * No patch ``` $ export DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1 $ ./manage.py makemigrations --dry-run /home/anel/mariadb/feedback-plugin-backend/env/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py:105: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': (2005, "Unknown MySQL server host 'db' (-3)") warnings.warn( Migrations for 'feedback_plugin': feedback_plugin/migrations/0002_alter_rawdata_upload_time.py - Alter field upload_time on rawdata ``` * With patch ``` $ ./manage.py makemigrations --dry-run Migrations for 'feedback_plugin': feedback_plugin/migrations/0002_alter_rawdata_upload_time.py - Alter field upload_time on rawdata ``` - Also server gets started correctly ``` $ ./manage.py runserver Performing system checks... System check identified no issues (0 silenced). You have 19 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, feedback_plugin, sessions. Run 'python manage.py migrate' to apply them. May 30, 2022 - 14:51:50 Django version 3.2.13, using settings 'feedback_plugin.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. ```
1 parent 7118c68 commit b21c80a

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/feedback_plugin/settings.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# SECURITY WARNING: don't run with debug turned on in production!
2626
DEBUG = bool(os.environ['DJANGO_DEBUG'])
2727

28-
ALLOWED_HOSTS = [os.environ['DJANGO_ALLOWED_HOSTS']]
28+
ALLOWED_HOSTS = os.environ['DJANGO_ALLOWED_HOSTS'].split(',')
2929

3030

3131
# Application definition
@@ -71,15 +71,16 @@
7171
]
7272

7373
WSGI_APPLICATION = 'feedback_plugin.wsgi.application'
74-
75-
74+
host_name = 'db'
75+
if ALLOWED_HOSTS:
76+
host_name = ALLOWED_HOSTS[0]
7677
DATABASES = {
7778
'default': {
7879
'ENGINE': 'django.db.backends.mysql',
7980
'NAME': os.environ['DJANGO_DB_NAME'],
8081
'USER': os.environ['DJANGO_DB_USER_NAME'],
8182
'PASSWORD': os.environ['DJANGO_DB_USER_PASSWORD'],
82-
'HOST': 'db',
83+
'HOST': host_name,
8384
'OPTIONS': {'charset': 'utf8',
8485
'use_unicode': True},
8586
'TEST': {

0 commit comments

Comments
 (0)