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
Dio Maulana
crud
Commits
dff378e7
Commit
dff378e7
authored
Feb 03, 2022
by
dio maulana
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
day 2
parent
eccb8e9f
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
291 additions
and
41 deletions
+291
-41
add.html
belajar/templates/belajar/add.html
+23
-0
delete.html
belajar/templates/belajar/delete.html
+15
-0
edit.html
belajar/templates/belajar/edit.html
+34
-0
index.html
belajar/templates/belajar/index.html
+49
-29
search.html
belajar/templates/belajar/search.html
+58
-0
urls.py
belajar/urls.py
+7
-1
views.py
belajar/views.py
+74
-8
settings.py
belajarcrud/settings.py
+5
-2
urls.py
belajarcrud/urls.py
+1
-1
base_template.html
templates/base_template.html
+25
-0
No files found.
belajar/templates/belajar/add.html
0 → 100644
View file @
dff378e7
{% 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
belajar/templates/belajar/delete.html
0 → 100644
View file @
dff378e7
{% 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
belajar/templates/belajar/edit.html
0 → 100644
View file @
dff378e7
{% 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 %}
belajar/templates/belajar/index.html
View file @
dff378e7
<!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"
>
<title>
Product
</title>
</head>
<body>
{% extends "base_template.html" %}
{% block content %}
<div
class=
"container"
>
<h1
class=
"mt-5"
>
Daftar Product
</h1>
<table
class=
"table mt-3"
>
<thead>
<h1
class=
"mt-5"
>
Products
</h1>
<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>
<tr>
<th
scope=
"col"
>
ID
</th>
<th
scope=
"col"
>
Name
</th>
<th
scope=
"col"
>
Category ID
</th>
<th
scope=
"col"
>
Action
</th>
<th
scope=
"col"
>
ID
</th>
<th
scope=
"col"
>
Name
</th>
<th
scope=
"col"
>
Category
</th>
<th
scope=
"col"
>
Action
</th>
</tr>
</thead>
<tbody>
</thead>
<tbody>
{% if latest_product_list %}
{% for product in latest_product_list %}
<tr>
<th
scope=
"row"
>
{{ product.id }}
</th>
<td>
{{ product.name_product }}
</td>
<td>
{{ product.category.name_category }}
</td>
<th
scope=
"row"
>
{{ product.id }}
</th>
<td>
{{ product.name_product }}
</td>
<td>
{{ product.category.name_category }}
</td>
<td>
<a
type=
"button"
class=
"btn btn-warning"
>
Edit
</a>
<a
type=
"button"
class=
"btn btn-danger"
>
Delete
</a>
<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 %}
{% endif %}
</tbody>
</tbody>
</table>
</div>
{% endblock %}
{% block script %}
{% endblock %}
</body>
</html>
\ No newline at end of file
belajar/templates/belajar/search.html
0 → 100644
View file @
dff378e7
{% 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
belajar/urls.py
View file @
dff378e7
...
...
@@ -3,5 +3,11 @@ from django.urls import path
from
.
import
views
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
belajar/views.py
View file @
dff378e7
from
django.shortcuts
import
render
from
django.shortcuts
import
render
,
redirect
# Create your views here.
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
Category
def
index
(
request
):
latest_product_list
=
Product
.
objects
.
order_by
(
'-id'
)[:
5
]
template
=
loader
.
get_template
(
'belajar/index.html'
)
context
=
{
'latest_product_list'
:
latest_product_list
,
}
return
HttpResponse
(
template
.
render
(
context
,
request
))
\ No newline at end of file
# latest_product_list = Product.objects.order_by('-id')
# template = loader.get_template('belajar/index.html')
# context = {
# 'latest_product_list': latest_product_list,
# }
# return HttpResponse(template.render(context, request))
# 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
)
belajarcrud/settings.py
View file @
dff378e7
...
...
@@ -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 = 'belajarcrud.urls'
TEMPLATES
=
[
{
'BACKEND'
:
'django.template.backends.django.DjangoTemplates'
,
'DIRS'
:
[],
'DIRS'
:
[
os
.
path
.
join
(
BASE_DIR
,
'templates'
)
],
'APP_DIRS'
:
True
,
'OPTIONS'
:
{
'context_processors'
:
[
...
...
belajarcrud/urls.py
View file @
dff378e7
...
...
@@ -17,6 +17,6 @@ from django.contrib import admin
from
django.urls
import
include
,
path
urlpatterns
=
[
path
(
'belajar/'
,
include
(
'belajar.urls
'
)),
path
(
'belajar/'
,
include
(
(
'belajar.urls'
,
'belajar'
),
namespace
=
'belajar
'
)),
path
(
'admin/'
,
admin
.
site
.
urls
),
]
templates/base_template.html
0 → 100644
View file @
dff378e7
<!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
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