Commit 22028fd3 authored by valdi's avatar valdi

commit crud search

parent 0f582f5d
......@@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
......@@ -55,7 +55,10 @@ ROOT_URLCONF = 'crud.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [
os.path.join(BASE_DIR, 'templates')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
......
......@@ -20,6 +20,6 @@ from django.urls import path
from django.urls.conf import include
urlpatterns = [
path('masterdata/', include(('masterdata.urls', 'masterdata'), namespace='masterdata')),
path('admin/', admin.site.urls),
path('masterdata/', include('masterdata.urls'))
]
......@@ -4,5 +4,8 @@ from django.contrib import admin
from django.contrib import admin
from .models import Category, Product
class CategoryAdmin(admin.ModelAdmin):
fields = ['category_text']
admin.site.register(Category)
admin.site.register(Product)
\ No newline at end of file
{% extends "base_template.html" %}
{% block content %}
<div class="container">
<form action="{% url 'masterdata:addData' %}" method="post">
{% csrf_token %}
<label for="product">Product : </label><br>
<input type="text" id="product" name="product"><br>
<label>Category : </label><br>
<select class="form-select" name="category" aria-label="Default select example">
{% for category in categories %}
<option value="{{ category.id }}">{{ category.category_text}}</option>
{% endfor %}
<input type="submit" class="btn btn-primary btn-sm" value="Add">
</form>
</div>
{% endblock %}
{% extends "base_template.html" %}
{% block content %}
<h1> Update Form </h1>
<form action="{% url 'masterdata:update' product_detail.id %}" method="POST">
{% csrf_token %}
<label>Name</label>
<input type="text" name="product" value="{{ product_detail.product_text }}">
<br><br>
<label>Category</label>
<select class="form-select" name="category" aria-label="Default select example">
{% for category in categories %}
<option value="{{ category.id }}">{{ category.category_text}}</option>
{% endfor %}
</select>
<input type="submit" value="Update">
</form>
{% endblock %}
\ No newline at end of file
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Daftar Menu</title>
<style>
body {
margin-top: 30px;
}
</style>
</head>
<body>
{% extends "base_template.html" %}
{% block content %}
<div class="container">
<h1>Daftar Product</h1>
<div>
<a href="#" class="btn btn-success">Add</a>
<div class="row">
<div class="col-md-3">
<a href="{% url 'masterdata:add' %}" class="btn btn-success">Add</a><div class="input-group rounded">
</div>
<form action="{% url 'masterdata:search' %}" method="get">
<input type="search" name="p" class="form-control rounded" placeholder="Search" aria-label="Search" aria-describedby="search-addon" />
<span class="input-group-text border-0" id="search-addon">
<i class="fas fa-search"></i>
</span>
</form>
</div>
</div>
<br>
......@@ -36,15 +29,39 @@
</thead>
<tbody>
<!-- daftar category-->
{% if category %}
{% for category in category %}
{% if latest_product_list %}
{% for product in latest_product_list %}
<tr>
<td>{{ category.category_text }}</td>
<td>{{ product.category.category_text }}</td>
<td>{{ product.product_text }}</td>
<td>
<a href="#" class="btn btn-warning btn-sm">Detail</a>
<a href="#" class="btn btn-primary btn-sm">Edit</a>
<a href="#" class="btn btn-danger btn-sm">Delete</a>
<a href="{% url 'masterdata:edit' product.id %}" class="btn btn-primary">Edit</a>
<!-- Button trigger modal -->
<button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ product.id }}">
Delete
</button>
<!-- Modal -->
<div class="modal fade" id="deleteModal{{ product.id }}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<form action="{% url 'masterdata: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">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Are u sure to delete?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Delete</button>
</div>
</div>
</div>
</form>
</div>
</td>
</tr>
{% endfor %}
......@@ -56,6 +73,4 @@
</tbody>
</table>
</div>
</body>
</html>
{% endblock %}
{% extends "base_template.html" %}
{% block content %}
<div class="container">
<h1>Daftar Product</h1>
<div class="row">
<div class="col-md-3">
<a href="{% url 'masterdata:add' %}" class="btn btn-success">Add</a><div class="input-group rounded">
</div>
<form action="{% url 'masterdata:search' %}" method="get">
<input type="search" name="p" class="form-control rounded" placeholder="Search" aria-label="Search" aria-describedby="search-addon" />
<span class="input-group-text border-0" id="search-addon">
<i class="fas fa-search"></i>
</span>
</form>
</div>
</div>
<br>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th width="30%">Category</th>
<th width="30%">Product</th>
<th width="30%">Option</th>
</tr>
</thead>
<tbody>
<!-- daftar category-->
{% if search %}
{% for product in search %}
<tr>
<td>{{ product.category.category_text }}</td>
<td>{{ product.product_text }}</td>
<td>
<a href="{% url 'masterdata:edit' product.id %}" class="btn btn-primary">Edit</a>
<!-- Button trigger modal -->
<button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ product.id }}">
Delete
</button>
<!-- Modal -->
<div class="modal fade" id="deleteModal{{ product.id }}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<form action="{% url 'masterdata: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">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Are u sure to delete?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Delete</button>
</div>
</div>
</div>
</form>
</div>
</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="5">Data tidak ditemukan.</td>
</tr>
{% endif %}
</tbody>
</table>
</div>
{% endblock %}
from django.urls import path
from django.contrib import admin
from masterdata import views
from . import views
from django.views import generic
from .views import index_view
app_name = 'masterdata'
urlpatterns = [
path('', index_view, name='index'),
path('', views.index, name='index'),
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.
from .models import Category, Product
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from django.shortcuts import get_object_or_404, render
from django.views import generic
def index_view(request):
category = Category.objects.all()
def index(request):
latest_product_list = Product.objects.all()
context = {
'category': category
'latest_product_list': latest_product_list,
}
return render(request, 'masterdata/index.html', context)
\ No newline at end of file
return render(request, 'masterdata/index.html', context)
def edit(request, product_id):
product_detail = Product.objects.get(pk=product_id)
categories = Category.objects.all()
context = {'product_detail': product_detail, 'categories': categories}
return render(request, 'masterdata/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, Product.DoesNotExist):
return HttpResponse('error')
else:
c = Product.objects.get(pk=product_id)
c.product_text = request.POST['product']
c.category_id = selected_category
c.save()
return redirect('masterdata:index')
def add(request):
categories = Category.objects.all()
context = {'categories': categories}
return render(request, 'masterdata/add.html', context)
def addData(request):
try:
selected_category = Category.objects.get(pk=request.POST['category'])
except (KeyError, Product.DoesNotExist):
return HttpResponse('error')
else:
p = Product()
p.product_text = request.POST['product']
p.category = selected_category
p.save()
return redirect('masterdata:index')
def delete(request, product_id):
try:
selected_category = Product.objects.get(pk=product_id)
except (KeyError, Product.DoesNotExist):
return HttpResponse('error')
else:
p = Product.objects.get(pk=product_id)
p.delete()
return redirect('masterdata:index')
def search(request):
getSearch = request.GET['p']
search = Product.objects.filter(product_text__icontains=getSearch)
context = {'search' : search}
return render(request, 'masterdata/search.html', context)
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<title>Daftar Menu</title>
<style>
body {
margin-top: 30px;
}
</style>
</head>
<body>
{% block content %}
{% 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