Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
crud
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ernanda
crud
Commits
22028fd3
Commit
22028fd3
authored
Feb 03, 2022
by
valdi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commit crud search
parent
0f582f5d
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
262 additions
and
41 deletions
+262
-41
settings.py
crud/settings.py
+5
-2
urls.py
crud/urls.py
+1
-1
admin.py
masterdata/admin.py
+3
-0
add.html
masterdata/templates/masterdata/add.html
+19
-0
edit.html
masterdata/templates/masterdata/edit.html
+17
-0
index.html
masterdata/templates/masterdata/index.html
+45
-30
search.html
masterdata/templates/masterdata/search.html
+76
-0
urls.py
masterdata/urls.py
+11
-3
views.py
masterdata/views.py
+60
-5
base_template.html
templates/base_template.html
+25
-0
No files found.
crud/settings.py
View file @
22028fd3
...
@@ -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 = 'crud.urls'
...
@@ -55,7 +55,10 @@ ROOT_URLCONF = 'crud.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'
:
[
...
...
crud/urls.py
View file @
22028fd3
...
@@ -20,6 +20,6 @@ from django.urls import path
...
@@ -20,6 +20,6 @@ from django.urls import path
from
django.urls.conf
import
include
from
django.urls.conf
import
include
urlpatterns
=
[
urlpatterns
=
[
path
(
'masterdata/'
,
include
((
'masterdata.urls'
,
'masterdata'
),
namespace
=
'masterdata'
)),
path
(
'admin/'
,
admin
.
site
.
urls
),
path
(
'admin/'
,
admin
.
site
.
urls
),
path
(
'masterdata/'
,
include
(
'masterdata.urls'
))
]
]
masterdata/admin.py
View file @
22028fd3
...
@@ -4,5 +4,8 @@ from django.contrib import admin
...
@@ -4,5 +4,8 @@ from django.contrib import admin
from
django.contrib
import
admin
from
django.contrib
import
admin
from
.models
import
Category
,
Product
from
.models
import
Category
,
Product
class
CategoryAdmin
(
admin
.
ModelAdmin
):
fields
=
[
'category_text'
]
admin
.
site
.
register
(
Category
)
admin
.
site
.
register
(
Category
)
admin
.
site
.
register
(
Product
)
admin
.
site
.
register
(
Product
)
\ No newline at end of file
masterdata/templates/masterdata/add.html
0 → 100644
View file @
22028fd3
{% 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 %}
masterdata/templates/masterdata/edit.html
0 → 100644
View file @
22028fd3
{% 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
masterdata/templates/masterdata/index.html
View file @
22028fd3
<!doctype html>
{% extends "base_template.html" %}
<html
lang=
"en"
>
{% block content %}
<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>
<div
class=
"container"
>
<div
class=
"container"
>
<h1>
Daftar Product
</h1>
<h1>
Daftar Product
</h1>
<div>
<div
class=
"row"
>
<a
href=
"#"
class=
"btn btn-success"
>
Add
</a>
<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>
</div>
<br>
<br>
...
@@ -36,15 +29,39 @@
...
@@ -36,15 +29,39 @@
</thead>
</thead>
<tbody>
<tbody>
<!-- daftar category-->
<!-- daftar category-->
{% if
category
%}
{% if
latest_product_list
%}
{% for
category in category
%}
{% for
product in latest_product_list
%}
<tr>
<tr>
<td>
{{ category.category_text }}
</td>
<td>
{{
product.
category.category_text }}
</td>
<td>
{{ product.product_text }}
</td>
<td>
{{ product.product_text }}
</td>
<td>
<td>
<a
href=
"#"
class=
"btn btn-warning btn-sm"
>
Detail
</a>
<a
href=
"{% url 'masterdata:edit' product.id %}"
class=
"btn btn-primary"
>
Edit
</a>
<a
href=
"#"
class=
"btn btn-primary btn-sm"
>
Edit
</a>
<!-- Button trigger modal -->
<a
href=
"#"
class=
"btn btn-danger btn-sm"
>
Delete
</a>
<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>
</td>
</tr>
</tr>
{% endfor %}
{% endfor %}
...
@@ -56,6 +73,4 @@
...
@@ -56,6 +73,4 @@
</tbody>
</tbody>
</table>
</table>
</div>
</div>
{% endblock %}
</body>
</html>
masterdata/templates/masterdata/search.html
0 → 100644
View file @
22028fd3
{% 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 %}
masterdata/urls.py
View file @
22028fd3
from
django.urls
import
path
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
=
[
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
masterdata/views.py
View file @
22028fd3
from
django.shortcuts
import
render
from
django.shortcuts
import
render
,
redirect
# Create your views here.
# Create your views here.
from
.models
import
Category
,
Product
from
.models
import
Category
,
Product
from
django.shortcuts
import
render
from
django.shortcuts
import
render
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
django.views
import
generic
def
index
_view
(
request
):
def
index
(
request
):
category
=
Category
.
objects
.
all
()
latest_product_list
=
Product
.
objects
.
all
()
context
=
{
context
=
{
'
category'
:
category
'
latest_product_list'
:
latest_product_list
,
}
}
return
render
(
request
,
'masterdata/index.html'
,
context
)
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
)
templates/base_template.html
0 → 100644
View file @
22028fd3
<!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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment