Reputation: 1
I am trying to store data from a form into my Database, I am using Django-Python for the backend.
Here is my HTML form
<form>
<center>
<div class="container-fluid">
<div class="row">
<div class="col-sm-5" style="background-color:white;">
<p>First name:<br></p>
</div>
<div class="col-sm-6" style="background-color:white;">
<p><input type="text" id="firstname"><br></p>
</div>
<div class="col-sm-5" style="background-color:white;">
<p>Last name:<br></p>
</div>
<div class="col-sm-6" style="background-color:white;">
<p><input type="text" id="lastname"><br></p>
</div>
<div class="col-sm-5" style="background-color:white;">
<p>Email ID:<br></p>
</div>
<div class="col-sm-6" style="background-color:white;">
<p><input type="text" id="email"><br></p>
</div>
<div class="col-sm-5" style="background-color:white;">
<p>Delivery Address:<br></p>
</div>
<div class="col-sm-6" style="background-color:white;">
<p><input type="text" id="address"><br></p>
</div>
<div class="col-sm-5" style="background-color:white;">
<p>Contact Number:<br></p>
</div>
<div class="col-sm-6" style="background-color:white;">
<p><input type="number" id="number"><br></p>
</div>
</div>
</div>
</center>
<h1> </h1>
<p>"Payment mode:- Cash On Delivery,</p>
<p>All payment have to be done to the delivery boy by cash."</p><br>
<center>
<button onclick = "submitform()" class="button" style="background-color:black;color:#ffcb04;padding:5px 10px 5px 10px;border-radius: 10px">Submit</button>
</center>
</form>
This is my javascript/Jquery/Ajax
function submitform(){
firstname = $("#firstname").val()
lastname = $("#lastname").val()
email = $("#email").val()
address = $("#address").val()
number = $("#number").val()
string = {
'firstname' :firstname,
'lastname' : lastname,
'email' : email,
'address' : address,
'number' : number
}
json_string = JSON.stringify(string);
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
url: '/orders/store-order-details/',
data: json_string,
success: function(data) {
window.location.href = "/confirmation/";
}
});
}
This is my Django views.py
def store_order_details(request):
received_json_data = json.loads(request.body)
customer_obj = Customer(first_name=received_json_data['first_name'], last_name=received_json_data['last_name'],
email_ID=received_json_data['email_id'], contact_number=received_json_data['number'],
address=received_json_data['address'])
print(customer_obj)
customer_obj.save()
print(received_json_data['data'])
return Response(status.HTTP_201_CREATED)
I am not able to figure out the bug in this as I feel this is alright at the superficial level.
The problem is that the data is not getting stored in the database. I am not sure if the data is posted by the Ajax. I feel the Python code in the view is fine. However I doubt if json.loads will work.
I get this error in the terminal
"POST /orders/store-order-details/ HTTP/1.1" 403 2274
When I test my view by using URL parameters
I get this error
the JSON object must be str, not 'bytes'
All form inputs are alphanumeric
Upvotes: 0
Views: 38
Reputation: 599876
The error has nothing to do with any of your code. The console is showing you have a 403 response, which is a Forbidden code; this is because Django protects POST requests from cross-site scripting attacks.
See the CSRF documentation for what to do.
Upvotes: 1