Commit dff378e7 authored by dio maulana's avatar dio maulana

day 2

parent eccb8e9f
{% extends "base_template.html" %}
{% block content %}
<div class="container mt-5">
<form action="{% url 'belajar:addData' %}" method="post">
{% csrf_token %}
<div class="mb-3">
<label class="form-label">Product Name</label>
<input type="text" class="form-control" name="product">
</div>
<div class="mb-3">
<label class="form-label">Category</label>
<select class="form-select" aria-label="Default select example" name="category">
{% for category in categories %}
<option value="{{ category.id }}">{{ category.name_category }}</option>
{% endfor %}
</select>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
{% endblock %}
\ No newline at end of file
{% extends "base_template.html" %}
{% block content %}
{% if product_detail %}
<div class="container mt-5">
<form action="{% url 'belajar:delete' product_detail.id %}" method="post">
{% csrf_token %}
<label class="form-label">Are you sure ?</label>
<div class="mb-3">
<a href="{% url 'belajar:products' %}" type="button" class="btn btn-info">Cancel</a>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</form>
</div>
{% endif %}
{% endblock %}
\ No newline at end of file
{% extends "base_template.html" %}
{% block content %}
{% if product_detail %}
<div class="container mt-5">
<form action="{% url 'belajar:update' product_detail.id %}" method="post">
{% csrf_token %}
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Product Name</label>
<input type="text" class="form-control" name="product_name" value="{{ product_detail.name_product }}">
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Category</label>
<select class="form-select" aria-label="Default select example" name="category">
{% for category in categories %}
{% if category.id == product_detail.category_id %}
<option value="{{ category.id }}" selected>{{ category.name_category }}</option>
{% else %}
<option value="{{ category.id }}">{{ category.name_category }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
{% endif %}
{% endblock %}
<!doctype html> {% extends "base_template.html" %}
<html lang="en"> {% block content %}
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Product</title>
</head>
<body>
<div class="container"> <div class="container">
<h1 class="mt-5">Daftar Product</h1> <h1 class="mt-5">Products</h1>
<table class="table mt-3" > <a type="button" href="{% url 'belajar:add' %}" class="btn btn-primary">Add Product</a>
<form action="{% url 'belajar:search' %}" method="get">
<input name="q" type="search" class="form-control mt-2" placeholder="Search Product Name...">
</form>
<table class="table mt-3">
<thead> <thead>
<tr> <tr>
<th scope="col">ID</th> <th scope="col">ID</th>
<th scope="col">Name</th> <th scope="col">Name</th>
<th scope="col">Category ID</th> <th scope="col">Category</th>
<th scope="col">Action</th> <th scope="col">Action</th>
</tr> </tr>
</thead> </thead>
...@@ -29,15 +23,41 @@ ...@@ -29,15 +23,41 @@
<td>{{ product.name_product }}</td> <td>{{ product.name_product }}</td>
<td>{{ product.category.name_category }}</td> <td>{{ product.category.name_category }}</td>
<td> <td>
<a type="button" class="btn btn-warning">Edit</a> <a href="{% url 'belajar:edit' product.id %}" type="button" class="btn btn-warning">Edit</a>
<a type="button" class="btn btn-danger">Delete</a> <button type="button" class="btn btn-danger" data-bs-toggle="modal"
data-bs-target=#deleteModal{{product.id }}>
Delete
</button>
</td> </td>
</tr> </tr>
<!-- Modal -->
<div class="modal fade" id="deleteModal{{product.id }}" tabindex="-1" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<form action="{% url 'belajar:delete' product.id %}" method="post">
{% csrf_token %}
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Are you sure to delete {{product.name_product}} ?</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</form>
</div>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
</tbody> </tbody>
</table> </table>
</div> </div>
{% endblock %}
{% block script %}
{% endblock %}
</body>
</html>
\ No newline at end of file
{% extends "base_template.html" %}
{% block content %}
<div class="container">
<h1 class="mt-5">Products</h1>
<a type="button" href="{% url 'belajar:products' %}" class="btn btn-success float-end">Back</a>
<a type="button" href="{% url 'belajar:add' %}" class="btn btn-primary">Add Product</a>
<form action="{% url 'belajar:search' %}" method="get">
<input name="q" type="text" class="form-control mt-2" placeholder="Search Product Name...">
</form>
<table class="table mt-3" >
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Category</th>
<th scope="col">Action</th>
</tr>
</thead>
{% if search %}
<tbody>
{% for product in search %}
<tr>
<th scope="row">{{ product.id }}</th>
<td>{{ product.name_product }}</td>
<td>{{ product.category.name_category }}</td>
<td>
<a href="{% url 'belajar:edit' product.id %}" type="button" class="btn btn-warning">Edit</a>
<button type="button" class="btn btn-danger" data-bs-toggle="modal"
data-bs-target=#deleteModal{{product.id }}>
Delete
</button>
</td>
</tr>
<!-- Modal -->
<div class="modal fade" id="deleteModal{{product.id }}" tabindex="-1" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<form action="{% url 'belajar:delete' product.id %}" method="post">
{% csrf_token %}
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Are you sure to delete {{product.name_product}} ?</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</form>
</div>
{% endfor %}
</tbody>
{% endif %}
</table>
</div>
{% endblock %}
\ No newline at end of file
...@@ -3,5 +3,11 @@ from django.urls import path ...@@ -3,5 +3,11 @@ from django.urls import path
from . import views from . import views
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('products', views.index, name='products'),
path('<int:product_id>/edit', views.edit, name='edit'),
path('<int:product_id>/update', views.update, name='update'),
path('add', views.add, name='add'),
path('addData', views.addData, name='addData'),
path('<int:product_id>/delete', views.delete, name='delete'),
path('search', views.search, name='search'),
] ]
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render, redirect
# Create your views here. # Create your views here.
from django.http import HttpResponse from django.http import HttpResponse
from django.template import loader # from django.template import loader
from django.shortcuts import get_object_or_404, render
from .models import Product from .models import Product
from .models import Category
def index(request): def index(request):
latest_product_list = Product.objects.order_by('-id')[:5] # latest_product_list = Product.objects.order_by('-id')
template = loader.get_template('belajar/index.html') # template = loader.get_template('belajar/index.html')
context = { # context = {
'latest_product_list': latest_product_list, # 'latest_product_list': latest_product_list,
} # }
return HttpResponse(template.render(context, request)) # return HttpResponse(template.render(context, request))
\ No newline at end of file
# short hand
latest_product_list = Product.objects.order_by('id')
context = {'latest_product_list': latest_product_list}
return render(request, 'belajar/index.html', context)
def edit(request, product_id):
# product_detail = Product.objects.raw(f'SELECT * FROM belajar_product WHERE id = {product_id}')
product_detail = Product.objects.get(id=product_id)
categories = Category.objects.all()
context = {'product_detail': product_detail, 'categories': categories}
return render(request, 'belajar/edit.html', context)
def update(request, product_id):
product = get_object_or_404(Product, pk=product_id)
try:
selected_category = Category.objects.get(pk=request.POST['category'])
except (KeyError, Category.DoesNotExist):
return HttpResponse('Cant update your action')
else:
q = Product.objects.get(pk=product_id)
q.name_product = request.POST['product_name']
q.category_id = selected_category
q.save()
return redirect('belajar:products')
def add(request):
categories = Category.objects.all()
context = {'categories': categories}
return render(request, 'belajar/add.html', context)
def addData(request):
try:
selected_category = Category.objects.get(pk=request.POST['category'])
except (KeyError, Category.DoesNotExist):
return HttpResponse('Category is not defined')
else:
product = Product()
product.name_product = request.POST['product']
product.category = selected_category
product.save()
return redirect('belajar:products')
def delete(request, product_id):
product = get_object_or_404(Product, pk=product_id)
try:
selected_product = Product.objects.get(pk=product_id)
except (KeyError, Product.DoesNotExist):
return HttpResponse("This product doesn't exist")
else:
q = Product.objects.get(pk=product_id)
q.delete()
return redirect('belajar:products')
def search(request):
getSearch = request.GET['q']
search = Product.objects.filter(name_product__icontains=getSearch)
context = {'search': search}
return render(request, 'belajar/search.html', context)
...@@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/4.0/topics/settings/ ...@@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/ https://docs.djangoproject.com/en/4.0/ref/settings/
""" """
import os
from pathlib import Path from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
...@@ -55,7 +55,10 @@ ROOT_URLCONF = 'belajarcrud.urls' ...@@ -55,7 +55,10 @@ ROOT_URLCONF = 'belajarcrud.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': [
os.path.join(BASE_DIR, 'templates')
],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [
......
...@@ -17,6 +17,6 @@ from django.contrib import admin ...@@ -17,6 +17,6 @@ from django.contrib import admin
from django.urls import include, path from django.urls import include, path
urlpatterns = [ urlpatterns = [
path('belajar/', include('belajar.urls')), path('belajar/', include(('belajar.urls', 'belajar'), namespace='belajar')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<title>Product</title>
</head>
<body>
{% block content %}
{% endblock %}
{% block script %}
{% endblock %}
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment