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

107
Visualizações
html form in Django returns none

I am trying to save this HTML form in my table in the database, but I am getting "None" error. I have been on this for some hour. Please any help will be appreciated enter image description here

Please, how do i fix this

The is the HTML order.html i created

 <form action="" method="post" id="payment-form">
                      <input type="text" class="form-control" id="first_name" 
                        name="first_name"
                        value="{{request.user.first_name}}">
                   
                    <textarea type="text" class="form-control" id="address" name="address" 
                    placeholder="1234 Main St" required></textarea>
               
                <button id="submit" class="btn btn-success lg w-100 fw-bold" >
                  Proceed to Payment
                </button>

</form>

Here is my views.py

def add(request):
    basket = Basket(request)
    if request.POST.get("action") == "post":
        order_key = request.POST.get("order_key")
        user_id = request.user.id
        baskettotal = basket.get_total_price()
        first_name = request.POST.get("first_name")
        last_name = request.POST.get("last_name")
        address = request.POST.get("address")
        print(first_name, last_name, address)

        order = Order.objects.create(
                    first_name = first_name,
                    address=address,
                    total_paid=baskettotal,
                    
            )
            order_id = order.pk
        response = JsonResponse({"success": "Order created"})
        return response

The js file


<script type="text/javascript">

  function makePayment(e) {
    e.preventDefault();
    
    $.ajax({
      type: "POST",
      url: '{% url "order:add" %}',
      data: {
        csrfmiddlewaretoken: "{{csrf_token}}",
        action: "post",
      },
      success: function (json) {
        console.log(json.success)
      },
      error: function (xhr, errmsg, err) {},
    });

   
  }
</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First of all you don't have to post extra value action to determine post request you just have to use request.method it will return you all available method you can check for specific method by adding check like this

if request.method == "POST":

note : allways use capitalized method while adding this check
and I'm not sure where you're calling makePayment() function and whatever data you're trying to access in your python script you've to pass it through ajax{data:{}} like this

 function makePayment(e) {
    e.preventDefault();
    
    $.ajax({
      type: "POST",
      url: '{% url "order:add" %}',
      data: {
        csrfmiddlewaretoken: "{{csrf_token}}",
        order_key : "your order key",
        first_name : $('#first_name').val(),
        last_name : $('#last_name').val(),
        address : $('#address').val()
      },
      success: function (json) {
        console.log(json.success)
      },
      error: function (xhr, errmsg, err) {},
    });
   
  }

and than access it like this

def add(request):
    basket = Basket(request)
    if request.method == "POST":
        order_key = request.POST.get("order_key")
        #....all other fields
about 4 years ago · Juan Pablo Isaza 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