【问题标题】:Django localhost refusing connection from another django localhostDjango localhost 拒绝来自另一个 django localhost 的连接
【发布时间】:2020-12-01 14:41:35
【问题描述】:

我有两个 django 应用程序,目前在 localhost 上运行。我计划将它们放在两台不同的服务器上,但目前它们正在我的机器上运行。

应用程序A 正在:8000 上运行

应用程序B 正在:8010 上运行

当我尝试使用以下命令从A 向B 发出请求时:

import requests

requests.post('http://0.0.0.0:8010/api/plan', data=post_data)

我收到连接错误:

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='0.0.0.0', port=8010): Max retries exceeded with url: /api/plan (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f82fed36630>: Failed to establish a new connection: [Errno 111] Connection refused',))

在我的B 应用程序中,我有ALLOWED_HOSTS = ['*']

有人知道我错过了什么吗?

【问题讨论】:

标签: django python-requests


【解决方案1】:

您应该将 cors-headers 添加到您的项目 settings.py 中

例如:

INSTALLED_APPS = [
    "corsheaders",
    # ...
]

MIDDLEWARE = [
    "corsheaders.middleware.CorsMiddleware",
    # ...
]
CORS_ALLOW_ALL_ORIGINS = True

CORS_ALLOW_ALL_METHODS = [
    "GET", "POST" # add allowed methods here
]

CORS_URLS_REGEX = r"^/api/.*$"

CORS_ALLOW_HEADERS = [
    "accept",
    "accept-encoding",
    "authorization",
    "content-type",
    "dnt",
    "origin",
    "user-agent",
    "x-csrftoken",
    "x-requested-with",
]

【讨论】:

  • 不幸的是,这对我没有任何改变。邮递员呼叫工作正常,但我仍然收到 django 请求的连接错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 2018-10-17
  • 2021-02-20
  • 2023-02-05
  • 2021-12-16
  • 1970-01-01
  • 2019-08-20
相关资源
最近更新 更多