Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

299
Views
formulario html en Django no devuelve ninguno

Estoy tratando de guardar este formulario HTML en mi tabla en la base de datos, pero recibo el error "Ninguno". He estado en esto durante una hora. Por favor cualquier ayuda será apreciada ingrese la descripción de la imagen aquí

Por favor, ¿cómo soluciono esto?

El es el order.html HTML que creé

 <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>

Aquí está mi vista.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

el archivo js

 <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>
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

En primer lugar, no tiene que publicar una acción de valor adicional para determinar la solicitud posterior, solo tiene que usar request.method , le devolverá todos los métodos disponibles que puede verificar para un método específico agregando un cheque como este

 if request.method == "POST":

nota : siempre use el método en mayúsculas al agregar este cheque
y no estoy seguro de dónde está llamando a la función makePayment() y cualquier dato al que intente acceder en su secuencia de comandos de Python, debe pasarlo a través de ajax{data:{}} como este

 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) {}, }); }

y luego acceder a él así

 def add(request): basket = Basket(request) if request.method == "POST": order_key = request.POST.get("order_key") #....all other fields
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!