Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
Django - taking values from POST request, JavaScript fetch API

Sorry if this is a noob question, I am creating a Django app and for that I am trying to access data received from POST request, using JavaScript fetch API but it is showing empty . I can't get what my mistake is. I have tried to remove all unnecessary parts to debug.The error is in this part:

Code of my views.py

def checkdb(request):
    if request.method == "POST":
        a = request.POST.get('tag', 'default')
        print("printing", a)
        print(request.POST)
    return HttpResponse("Hello")

def check(request):
    return render(request, 'shop/new.html')

Code of URLS.py

urlpatterns = [
    path('', views.index, name="shop"),
    path('checkdb/', views.checkdb, name="checkdb"),
    path('check/', views.check, name="check"),
]

Code of new.html, it has only script tag to fetch request just for testing purpose.

<script>

data = JSON.stringify({
    headline: "Testing",
    tag: "Testing",
    background_image: "Testing",
    content: "Testing",
    user: 1
})

let csrftoken = getCookie('csrftoken');
let response = fetch("/shop/checkdb/", {
    method: 'POST',
    body: data,
    headers: { 'Accept': 'application/json, text/plain, */*',
        'Content-Type': 'application/json',
        "X-CSRFToken": csrftoken },
})


function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i].trim();
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

</script>

When i go to endpoint shop/check then its script tag executes and there is no error in console but when i try to print the data received in terminal then it always prints default option i am not getting what is mistake I want to print the data corresponding to key "tag" which is "Testing" but it is giving default option Please help me to find out my mistake.

OUTPUT in terminal

Quit the server with CTRL-BREAK.
[01/May/2020 19:09:02] "GET /shop/check/ HTTP/1.1" 200 1091
printing default
<QueryDict: {}>
[01/May/2020 19:09:02] "POST /shop/checkdb/ HTTP/1.1" 200 5

I am using Django 3.0.4.

Thanks and sorry if there is any silly mistake, i am a newbie in Django

over 4 years ago · Santiago Trujillo
5 Respostas
Responde à pergunta

0

As per my research request.POST won't work it only works when there is form data, i solved my issue of accessing the data by using

 data = json.loads(request.body.decode("utf-8"))
 tag = data['tag']
 print(data)
 print(tag)

And to use json.loads() first you have to import json module.

over 4 years ago · Santiago Trujillo Relatório

0

According to documentation:

json.loads() Deserialize s (a str, bytes or bytearray instance containing a JSON document) to a Python object using this conversion table.

The data inside request.body is of bytes type. As json.loads already accepts bytes type of data, we do not need to decode it. So, we can simply write:

data = json.loads(request.data)
print(data) # We get data as a dictionary 
over 4 years ago · Santiago Trujillo Relatório

0

In my experience, data sent using fetch are not located inside request.POST but rather inside request.body. There are then cases where the received data is in byte so you will need to decode it first. I suggest that you do this first:

if request.method == "POST":
    import json
    post_data = json.loads(request.body.decode("utf-8"))

Then try accessing post_data content like a normal python dictionary to get the value of tag as in post_data.get("tag")

over 4 years ago · Santiago Trujillo Relatório

0

The view received the data from the AJAX POST request and the data was in the JSON format, so we need to load it using the json module from the standard library:

import json

def my_view(request):
    if request.method == 'POST':
        # get the data from the ajax post request
        data = json.load(request) # dictionary
        tag = data.get('tag')
over 4 years ago · Santiago Trujillo Relatório

0

Just to mention, in my experience:

import json


def some_view(request):
    # data = request.POST  # Is going to work for the test client
    data = json.loads(request.body)  # Works, but for the test client it doesn't
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda