小编Lea*_*ner的帖子

requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8000): 最大重试次数超过 url: /api/1/

我正在尝试在DjangoPython文件之间进行通信,但出现以下错误:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded with url: /api/1/ (Caused by NewConnectionError(': Failed to build a new connection: [WinError 10061]无法建立连接,因为目标机器主动拒绝它'))

我创建了一个名为test.pyIn a Djangoapp的 Python 文件并尝试在它们之间进行通信。我的Python 文件包含以下代码:

import requests
BASE_URL='http://127.0.0.1:8000/'
ENDPOINT='api/'
def get_resource(id):
    resp=requests.get(BASE_URL+ENDPOINT+id+'/')
    print(resp.status_code)
    print(resp.json()) 
id=input("Enter some ID: ")
get_resource(id)
Run Code Online (Sandbox Code Playgroud)

Models.py 包含:-

from django.db import models

# Create your models here.
class Employee(models.Model):
    eno=models.IntegerField()
    ename=models.CharField(max_length=70)
    esal=models.FloatField()
    eaddr=models.CharField(max_length=100)
Run Code Online (Sandbox Code Playgroud)

Admin.py 包含:-

from django.contrib import admin
from testapp.models import Employee
# Register your …
Run Code Online (Sandbox Code Playgroud)

python api django django-rest-framework

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

api ×1

django ×1

django-rest-framework ×1

python ×1