Ошибка запроса View.py: Failed to establish a new connection: [Errno 111] Connection refused
Я установил docker для моих 2 django проектов (frontend | backend)
это мой yaml
`version: "3.12"
services:
mysql:
image: mysql:8.0
container_name: mysql_db
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_DATABASE: gfmic_frontend_db_v1
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
networks:
- backend
event_backend:
build: ./GFMIC_Backend
container_name: GFMIC_Backend
depends_on:
- mysql
volumes:
- ./GFMIC_Backend:/django
ports:
- "8001:8001"
expose:
- 8001
environment:
- DJANGO_SETTINGS_MODULE=GFMIC_Backend.settings
- DB_HOST=mysql # Service name for MySQL container
- DB_NAME=gfmic_backend_db_v1
- DB_USER=root
- DB_PASSWORD= # Empty password
- SECRET_KEY=SECRET
- DEBUG=True
- ALLOWED_HOSTS=127.0.0.1 event_backend event_frontend localhost 0.0.0.0 host.docker.internal
networks:
- backend
event_frontend:
build: ./GFMIC_Frontend
container_name: GFMIC_Frontend
depends_on:
- mysql
ports:
- "8000:8000"
expose:
- 8000
volumes:
- ./GFMIC_Frontend:/django
environment:
- DJANGO_SETTINGS_MODULE=GFMIC_Frontend.settings
- DB_HOST=mysql # Service name for MySQL container
- DB_NAME=gfmic_frontend_db_v1
- DB_USER=root
- DB_PASSWORD= # Empty password
- SECRET_KEY=SECRET
- DEBUG=True
- ALLOWED_HOSTS=127.0.0.1 event_backend event_frontend localhost 0.0.0.0 host.docker.internal
networks:
- backend
volumes:
mysql_data:
networks:
backend:
driver: bridge
`
Я получил ошибку
"Error fetching events: HTTPConnectionPool(host='127.0.0.1', port=8001): Max retries exceeded with url: /Event_Management/api/EventsDetails/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f85a023b710>: Failed to establish a new connection: [Errno 111]. Connection refused'))"
когда я пытаюсь выполнить api запрос к моему views.py
Пример:
`def events_list(request):
title= "Events List"
api_url = 'http://127.0.0.1:8001/Event_Management/api/EventsDetails/' # Replace with your API endpoint
try:
response = requests.get(api_url)
response.raise_for_status() # Raises an HTTPError for bad responses (4xx and 5xx)
events = response.json() # Parse the JSON response
except requests.exceptions.RequestException as e:
# Handle request exceptions (e.g., network errors)
events = []
# Optionally log the error or provide feedback to the user
print(f"Error fetching events: {e}")
return render(request, 'events/events_list.html', {'events': events, 'title': title})`
Я также пробую http://event_backend:8001/Event_Management/api/EventsDetails/
Он показывает:
django.core.exceptions.DisallowedHost: Недопустимый заголовок HTTP_HOST: 'event_backend:8001'. Указанное доменное имя не является действительным в соответствии с RFC 1034/1035.
но в javascript через ajax это работает:
Надеемся решить эту проблему
РЕШЕНО:
ПРОСТО НЕ ИСПОЛЬЗУЙТЕ UNDERSCORE В ИМЕНИ АППАРАТА В ДОКЕРЕ ИСПОЛЬЗУЙТЕ "eventbackend" ВМЕСТО "event_backend"
ЗА Ф "_" 3 часа отладки