Skip to content

Commit

Permalink
Merge pull request #7 from fathonidf/development
Browse files Browse the repository at this point in the history
selesai tutorial 5
  • Loading branch information
fathonidf authored Oct 4, 2023
2 parents 7b61f72 + 543b21d commit 8506bdf
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 6 deletions.
83 changes: 80 additions & 3 deletions main/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h5>Name:</h5>
<h5>Class:</h5>
<p>{{class}}</p>

<table>
<!-- <table>
<tr>
<th>Name</th>
<th>Price</th>
Expand Down Expand Up @@ -65,16 +65,93 @@ <h5>Class:</h5>
</td>
</tr>
{% endfor %}
</table>
</table> -->

<table id="product_table"></table>

<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Add New Product</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="form" onsubmit="return false;">
{% csrf_token %}
<div class="mb-3">
<label for="name" class="col-form-label">Name:</label>
<input type="text" class="form-control" id="name" name="name"></input>
</div>
<div class="mb-3">
<label for="price" class="col-form-label">Price:</label>
<input type="number" class="form-control" id="price" name="price"></input>
</div>
<div class="mb-3">
<label for="description" class="col-form-label">Description:</label>
<textarea class="form-control" id="description" name="description"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="button_add" data-bs-dismiss="modal">Add Product</button>
</div>
</div>
</div>
</div>

<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">Add Product by AJAX</button>

<br />

<h5>Sesi terakhir login: {{ last_login }}</h5>

<a href="{% url 'main:create_product' %}">
<button>
Add New Product
</button>
</a>

<h5>Sesi terakhir login: {{ last_login }}</h5>

<script>
async function getProducts() {
return fetch("{% url 'main:get_product_json' %}").then((res) => res.json())
}
async function refreshProducts() {
document.getElementById("product_table").innerHTML = ""
const products = await getProducts()
let htmlString = `<tr>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>Date Added</th>
</tr>`
products.forEach((item) => {
htmlString += `\n<tr>
<td>${item.fields.name}</td>
<td>${item.fields.price}</td>
<td>${item.fields.description}</td>
<td>${item.fields.date_added}</td>
</tr>`
})

document.getElementById("product_table").innerHTML = htmlString
}

refreshProducts()

function addProduct() {
fetch("{% url 'main:add_product_ajax' %}", {
method: "POST",
body: new FormData(document.querySelector('#form'))
}).then(refreshProducts)

document.getElementById("form").reset()
return false
}

document.getElementById("button_add").onclick = addProduct
</script>

{% endblock content %}
7 changes: 5 additions & 2 deletions main/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.urls import path
from main.views import show_main, create_product, show_xml, show_json, show_xml_by_id, show_json_by_id, register, login_user, logout_user, edit_product, delete_product
from main.views import show_main, create_product, show_xml, show_json, show_xml_by_id, show_json_by_id, register, login_user, logout_user, edit_product, delete_product, \
get_product_json, add_product_ajax

app_name = 'main'

Expand All @@ -14,5 +15,7 @@
path('json/', show_json, name='show_json'),
path('xml/<int:id>/', show_xml_by_id, name='show_xml_by_id'),
path('json/<int:id>/', show_json_by_id, name='show_json_by_id'),
path('', show_main, name='show_main')
path('', show_main, name='show_main'),
path('get-product/', get_product_json, name='get_product_json'),
path('create-product-ajax/', add_product_ajax, name='add_product_ajax')
]
22 changes: 21 additions & 1 deletion main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt

# Create your views here.
@login_required(login_url='/login')
Expand Down Expand Up @@ -107,4 +108,23 @@ def show_xml_by_id(request, id):

def show_json_by_id(request, id):
data = Product.objects.filter(pk=id)
return HttpResponse(serializers.serialize("json", data), content_type="application/json")
return HttpResponse(serializers.serialize("json", data), content_type="application/json")

def get_product_json(request):
product_item = Product.objects.filter(user=request.user)
return HttpResponse(serializers.serialize('json', product_item))

@csrf_exempt
def add_product_ajax(request):
if request.method == 'POST':
name = request.POST.get("name")
price = request.POST.get("price")
description = request.POST.get("description")
user = request.user

new_product = Product(name=name, price=price, description=description, user=user)
new_product.save()

return HttpResponse(b"CREATED", status=201)

return HttpResponseNotFound()

0 comments on commit 8506bdf

Please sign in to comment.