From 543b21d34873d0a3fb1e169e2e8d9b2698152a7e Mon Sep 17 00:00:00 2001 From: fathonidf Date: Wed, 4 Oct 2023 15:41:12 +0700 Subject: [PATCH] selesai tutorial 5 --- main/templates/main.html | 83 ++++++++++++++++++++++++++++++++++++++-- main/urls.py | 7 +++- main/views.py | 22 ++++++++++- 3 files changed, 106 insertions(+), 6 deletions(-) diff --git a/main/templates/main.html b/main/templates/main.html index 23b5437..34107f7 100644 --- a/main/templates/main.html +++ b/main/templates/main.html @@ -32,7 +32,7 @@
Name:
Class:

{{class}}

- + + +
+ + + +
-
Sesi terakhir login: {{ last_login }}
+
Sesi terakhir login: {{ last_login }}
+ + + {% endblock content %} \ No newline at end of file diff --git a/main/urls.py b/main/urls.py index 7f0f2c9..a328b0f 100644 --- a/main/urls.py +++ b/main/urls.py @@ -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' @@ -14,5 +15,7 @@ path('json/', show_json, name='show_json'), path('xml//', show_xml_by_id, name='show_xml_by_id'), path('json//', 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') ] \ No newline at end of file diff --git a/main/views.py b/main/views.py index 5118f12..394fd5d 100644 --- a/main/views.py +++ b/main/views.py @@ -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') @@ -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") \ No newline at end of file + 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() \ No newline at end of file