使用Postman测试Django REST Framework API时无法连接127.0.0.1:8000
Hey there, let's troubleshoot why Postman can't connect to your Django dev server at http://127.0.0.1:8000/api/! Here are the most common fixes to walk through:
First, confirm your Django server is running
Check the terminal where you launched Django—you should see a line likeStarting development server at http://127.0.0.1:8000/. If not, runpython manage.py runserver(orpython3 manage.py runserverdepending on your environment) from your project root directory to start it up.Fix the URL typo (this is probably your issue!)
The error message shows you triedhttp://127.0.0.1.8000/api/—notice the dot instead of a colon between the IP and port? That's invalid. The correct URL should behttp://127.0.0.1:8000/api/(use:to separate the IP address and port number). Also double-check that the/api/path is properly defined in your Djangourls.pyfile.Check if port 8000 is occupied
Sometimes another program might be using port 8000, preventing Django from binding to it. Try starting the server on a different port:python manage.py runserver 8001Then test with
http://127.0.0.1:8001/api/in Postman. If that works, you can find and kill the process using port 8000:- Windows:
netstat -ano | findstr :8000thentaskkill /PID <process-id> /F - Mac/Linux:
lsof -i :8000thenkill -9 <process-id>
- Windows:
Verify Postman proxy settings
Postman's proxy configuration can block local connections. Go to Settings → Proxy and:- Turn off "Use system proxy" if it's enabled
- Add
127.0.0.1to the "No proxy for" list
Rule out firewall/antivirus interference
Your local firewall or antivirus might be blocking traffic between Postman and Django. Temporarily disable them to test—if the connection works, add port 8000 to your security tool's allowed list.Test with your browser first
Try visitinghttp://127.0.0.1:8000/api/in your web browser. If the browser can't reach it either, the problem is with your Django setup (not Postman). If the browser works, double-check Postman's request method, headers, and URL again.
内容的提问来源于stack exchange,提问作者pratik veer

