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

104
Views
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 answers
Answer question

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