Skip to content

Commit

Permalink
2 - Ajax-ify a Form
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Oct 6, 2017
1 parent 37ba016 commit 1d4d7ca
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/carts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def cart_home(request):

def cart_update(request):
product_id = request.POST.get('product_id')
if request.is_ajax():
print("Ajax request")
if product_id is not None:
try:
product_obj = Product.objects.get(id=product_id)
Expand Down
Binary file modified src/db.sqlite3
Binary file not shown.
2 changes: 1 addition & 1 deletion src/products/templates/products/snippets/update-cart.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form method='POST' action='{% url "cart:update" %}' class="form"> {% csrf_token %}
<form class='form-product-ajax' method='POST' action='{% url "cart:update" %}' class="form"> {% csrf_token %}
<input type='hidden' name='product_id' value='{{ product.id }}' />

{% if in_cart %}
Expand Down
35 changes: 35 additions & 0 deletions src/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,40 @@


{% include 'base/js.html' %}

<script>
$(document).ready(function(){
var productForm = $(".form-product-ajax") // #form-product-ajax

productForm.submit(function(event){
event.preventDefault();
console.log("Form is not sending")
var thisForm = $(this)
var actionEndpoint = thisForm.attr("action");
var httpMethod = thisForm.attr("method");
var formData = thisForm.serialize();

$.ajax({
url: actionEndpoint,
method: httpMethod,
data: formData,
success: function(data){
console.log("success")
console.log(data)
},
error: function(errorData){
console.log("error")
console.log(errorData)
}
})

})




})

</script>
</body>
</html>
5 changes: 3 additions & 2 deletions src/templates/base/js.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% load static %}
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<!-- jQuery first, then Popper.js, then Bootstrap JS
slim is missing ajax -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>

0 comments on commit 1d4d7ca

Please sign in to comment.