I am new to Django and even newer to ESP8266. I'm trying to send information from ESP8266 to my Django server using the POST method. The information looks like this:
Temperature=24.60&Humiditi=30.30&Soil=0.00&Water=0&Light=602.00
So my ESP8266 code looks like this:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#define POMPA1 D0
const char* ssid = "****";
const char* password = "*****";
String serverName = "http://127.0.0.1:8000/data/";
int POMP1 = HIGH;
char output;
String output_str;
String payload;
String server_output = "rain";
unsigned long lastTime = -20000;
unsigned long currentTime = 0;
unsigned long remeberedTime = 0;
int timeDelay = 20000;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(POMPA1, OUTPUT);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(2000);
    Serial.print(".");
    Serial.println("");
    Serial.print("Connected to WiFi network with IP Adress: ");
    Serial.println(WiFi.localIP());
    Serial.print("DNS IP: ");
    Serial.println(WiFi.dnsIP());
  }
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}
void loop() { // run over and over
  if (Serial.available()) {
    output_str = Serial.readString();
    Serial.println(output_str);
      if (WiFi.status() == WL_CONNECTED) {
        HTTPClient http;
        http.begin(serverName);
        http.addHeader("Content-Type", "application/x-www-form-urlencoded");
        int httpCode = http.POST(output_str);
        payload = http.getString();
        Serial.println(httpCode);
        Serial.println(payload);
        http.end();
      }
      else {
        Serial.println("Server Disconnected");
      }
  }
  Serial.println(payload);
  if (payload=="pompa1"){
    currentTime = millis();
    Serial.println("Let's rain");
    if (currentTime >= lastTime + timeDelay) {
      remeberedTime = currentTime;
      lastTime = currentTime + 10000;
    }
    if (millis() - remeberedTime <= 10000){
      digitalWrite(POMPA1, LOW);
    }
    else {
      digitalWrite(POMPA1, HIGH);
    }
  }
  delay(5000);
}
And the output in the console is :
-> Connected to WiFi network with IP Adress: 192.168.0.101
-> DNS IP: 192.168.0.1
So that's why I know that there is no problem with the server connection. Next here are my urls:
urlpatterns =[
    path("", data, name="main-page"),
    path("data/", get_data, name="get-data")
]
And here are my views:
def data(request):
    if request.method=="GET":
        return HttpResponse("<h1>Tutaj będą twoje wyniki</h1>")
def get_data(request):
    if request.method=="POST":
        print(request)
        return HttpResponse("<h1>Tutaj są twoje wyniki </h1>")
For now, I just want to somehow get the information from ESP to the Django server so next, I can save them in my database. Does someone know where the problem is?
 
                        
Guys up there get me back on the right track. They were right that I was giving the wrong address because 127.0.0.1 is for application on local host. So first of all I needed to change the address to 0.0.0.0 to allow connection to the server to other devices connected to the net.
then I needed to check IP of my computer because it was hosting the server so this supposed to be IP I should connect. I checked the IP using (for Linux):
then I needed to add addresses to Django settings in settings.py
then I only needed to change the code for ESP82666 to connect to proper IP:
And it worked perfectly! Thank everyone to contribute finally I can move forward with my project.