gamingflexer commited on
Commit
8c8a524
·
1 Parent(s): ca2ef86

Add delete_product_api view

Browse files
Files changed (2) hide show
  1. src/app/api/urls.py +1 -0
  2. src/app/api/views.py +11 -1
src/app/api/urls.py CHANGED
@@ -9,4 +9,5 @@ urlpatterns = [
9
  path('product/<int:product_id>/edit/', views.edit_product, name='edit_product'),
10
  path('upload/', views.upload_image_and_audio, name='add_product_by_image'),
11
  path('api/products/', ProductAPIView.as_view(), name='product_api'),
 
12
  ]
 
9
  path('product/<int:product_id>/edit/', views.edit_product, name='edit_product'),
10
  path('upload/', views.upload_image_and_audio, name='add_product_by_image'),
11
  path('api/products/', ProductAPIView.as_view(), name='product_api'),
12
+ path('api/delete_product/<int:product_id>/', views.delete_product_api, name='delete_product_api'),
13
  ]
src/app/api/views.py CHANGED
@@ -12,6 +12,8 @@ from django.http import JsonResponse
12
  import os
13
  from config import BASE_PATH
14
  from api.module.product_description import get_details
 
 
15
 
16
  def catalouge_page(request):
17
  # Fetch all products from the database
@@ -130,4 +132,12 @@ def edit_voice_product(request,product_id):
130
  else:
131
  return JsonResponse({'error': 'No voice file submitted.'}, status=400)
132
  else:
133
- return render(request, 'edit_voice_product.html')
 
 
 
 
 
 
 
 
 
12
  import os
13
  from config import BASE_PATH
14
  from api.module.product_description import get_details
15
+ from django.views.decorators.http import require_http_methods
16
+
17
 
18
  def catalouge_page(request):
19
  # Fetch all products from the database
 
132
  else:
133
  return JsonResponse({'error': 'No voice file submitted.'}, status=400)
134
  else:
135
+ return render(request, 'edit_voice_product.html')
136
+
137
+ def delete_product_api(request, product_id):
138
+ try:
139
+ product = Product.objects.get(pk=product_id)
140
+ product.delete()
141
+ return HttpResponseRedirect(reverse('index'))
142
+ except Product.DoesNotExist:
143
+ return JsonResponse({'error': 'Product not found.'}, status=404)