
[英]You must set settings.ALLOWED_HOSTS if DEBUG is False. Django-r-f
[英]Docker Django Error: CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False
我一直在尝试将我的 django 项目与 postgresql 对接,但一次又一次地遇到同样的问题。
web_1 | CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
我将 a.env 中的环境变量设置为:
环境:
DEBUG = True
ALLOWED_HOSTS=localhost 127.0.0.1 0.0.0.0:8000
并使用 python-decouple 模块在 settings.py 中配置它们
from decouple import config
DEBUG = config('DEBUG', default=False)
ALLOWED_HOSTS = config('ALLOWED_HOSTS').split(sep=' ')
运行项目:
python manage.py runserver
工作得很好,没有错误。 但是,说到跑步:
docker-compose up
我得到以下 output
Starting postgres_db ... done
Starting project_web_1 ... done
Attaching to postgres_db, project_web_1
postgres_db |
postgres_db | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_db |
postgres_db | 2021-07-30 17:58:52.695 UTC [1] LOG: starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_db | 2021-07-30 17:58:52.696 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_db | 2021-07-30 17:58:52.696 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_db | 2021-07-30 17:58:52.747 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_db | 2021-07-30 17:58:52.887 UTC [27] LOG: database system was shut down at 2021-07-30 17:58:34 UTC
postgres_db | 2021-07-30 17:58:52.950 UTC [1] LOG: database system is ready to accept connections
web_1 | CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
project_web_1 exited with code 1
我试过设置ALLOWED_HOSTS=['*']
并且也包括其他主机,但我找不到任何有效的东西。
Dockerfile
FROM python:3.9
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN pip install -r requirements.txt
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
docker-compose.yml
version: "3.8"
services:
web:
build: .
volumes:
- .:/django
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
container_name: postgres_db
所以我没有找到解决问题的明显方法,因此,看到逻辑上没有任何错误,我决定重新启动整个项目(从 docker 容器开始)并复制我需要的 django 文件。 它现在就像一个魅力。
我感谢那些花时间回答的人。 遵循https://docs.docker.com/samples/django/作为@markwalker_ 上面的建议。
出于某种奇怪的原因,它仅在我使用 gunicorn 启动 django 和 nginx idk 时才对我有用,我什至在其他地方克隆了我的存储库但没有效果,我也启动了 django shell 并且设置为 debug is False 尽管它被硬编码为 True .. . 这件事毁了我的一天(:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.