Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
latihan-irell
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
Aurellesia
latihan-irell
Commits
faa7ae9f
Commit
faa7ae9f
authored
Jan 27, 2022
by
aurellesia
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
generic view
parent
f14b1ea3
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
84 additions
and
96 deletions
+84
-96
forms.cpython-38.pyc
prodcat/__pycache__/forms.cpython-38.pyc
+0
-0
models.cpython-38.pyc
prodcat/__pycache__/models.cpython-38.pyc
+0
-0
urls.cpython-38.pyc
prodcat/__pycache__/urls.cpython-38.pyc
+0
-0
views.cpython-38.pyc
prodcat/__pycache__/views.cpython-38.pyc
+0
-0
forms.py
prodcat/forms.py
+13
-15
models.py
prodcat/models.py
+8
-1
categoryAdd.html
prodcat/templates/prodcat/categoryAdd.html
+1
-1
categoryIndex.html
prodcat/templates/prodcat/categoryIndex.html
+1
-1
productAdd.html
prodcat/templates/prodcat/productAdd.html
+2
-46
productEdit.html
prodcat/templates/prodcat/productEdit.html
+8
-0
productIndex.html
prodcat/templates/prodcat/productIndex.html
+8
-3
urls.py
prodcat/urls.py
+7
-4
views.py
prodcat/views.py
+36
-25
No files found.
prodcat/__pycache__/forms.cpython-38.pyc
View file @
faa7ae9f
No preview for this file type
prodcat/__pycache__/models.cpython-38.pyc
View file @
faa7ae9f
No preview for this file type
prodcat/__pycache__/urls.cpython-38.pyc
View file @
faa7ae9f
No preview for this file type
prodcat/__pycache__/views.cpython-38.pyc
View file @
faa7ae9f
No preview for this file type
prodcat/forms.py
View file @
faa7ae9f
...
@@ -11,21 +11,19 @@ class CategoryForm(forms.ModelForm):
...
@@ -11,21 +11,19 @@ class CategoryForm(forms.ModelForm):
}
}
}
}
class
DateInput
(
forms
.
DateInput
):
input_type
=
'date'
class
ProductForm
(
forms
.
ModelForm
):
class
ProductForm
(
forms
.
ModelForm
):
class
Meta
:
class
Meta
:
model
=
Products
model
=
Products
fields
=
"__all__"
fields
=
[
error_messages
=
{
'name'
,
'name'
:
{
'price'
,
'required'
:
'Name cannot be empty'
'restock_date'
,
},
'category'
'price'
:
{
]
'required'
:
'Price cannot be empty'
def
__init__
(
self
,
*
args
,
**
kwargs
):
},
super
(
ProductForm
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
'restock_date'
:{
self
.
fields
[
'restock_date'
]
=
forms
.
DateTimeField
(
widget
=
DateInput
())
'required'
:
'Restock date cannot be empty'
self
.
fields
[
'category'
]
=
forms
.
ModelChoiceField
(
widget
=
forms
.
Select
,
queryset
=
Category
.
objects
.
all
(),
required
=
False
)
},
\ No newline at end of file
'category'
:{
'required'
:
'Category cannot be empty'
}
}
\ No newline at end of file
prodcat/models.py
View file @
faa7ae9f
from
django.db
import
models
from
django.db
import
models
from
django.urls
import
reverse
# Create your models here.
# Create your models here.
class
Category
(
models
.
Model
):
class
Category
(
models
.
Model
):
category_text
=
models
.
CharField
(
max_length
=
200
)
category_text
=
models
.
CharField
(
max_length
=
200
)
...
@@ -15,6 +15,13 @@ class Products(models.Model):
...
@@ -15,6 +15,13 @@ class Products(models.Model):
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
name
return
self
.
name
def
restockdate
(
self
):
return
self
.
restock_date
.
strftime
(
'
%
Y-
%
m-
%
d'
)
def
get_absolute_url
(
self
):
return
reverse
(
'product-create'
,
kwargs
=
{
'pk'
:
self
.
pk
})
...
...
prodcat/templates/prodcat/categoryAdd.html
View file @
faa7ae9f
...
@@ -17,7 +17,7 @@
...
@@ -17,7 +17,7 @@
{% csrf_token %}
{% csrf_token %}
<table
style=
"margin:20px auto;"
>
<table
style=
"margin:20px auto;"
>
<tr>
<tr>
<td>
Category Name
eeeeeeeeeee
</td>
<td>
Category Name
</td>
<td>
<td>
<input
type=
"text"
name=
"category_text"
value=
""
></td>
<input
type=
"text"
name=
"category_text"
value=
""
></td>
</tr>
</tr>
...
...
prodcat/templates/prodcat/categoryIndex.html
View file @
faa7ae9f
...
@@ -10,7 +10,7 @@
...
@@ -10,7 +10,7 @@
</tr>
</tr>
</thead>
</thead>
<tbody>
<tbody>
{% for item in
data
%}
{% for item in
category_list
%}
<tr>
<tr>
<td>
{{item.category_text}}
</td>
<td>
{{item.category_text}}
</td>
<td>
<td>
...
...
prodcat/templates/prodcat/productAdd.html
View file @
faa7ae9f
<div>
<div>
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div>
<strong>
{{ error|escape }}
</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div
>
<strong>
{{ error|escape }}
</strong>
</div>
{% endfor %}
{% endif %}
<form
method=
"post"
>
<form
method=
"post"
>
{% csrf_token %}
{% csrf_token %}
<table
style=
"margin:20px auto;"
>
{{ form.as_p }}
<tr>
<input
type=
"submit"
value=
"Save"
>
<td>
Name
</td>
<td>
<input
type=
"text"
name=
"name"
value=
""
></td>
</tr>
<tr>
<td>
Price
</td>
<td>
<input
type=
"number"
name=
"price"
value=
""
></td>
</tr>
<tr>
<td>
Restock Date
</td>
<td>
<input
type=
"date"
name=
"restock_date"
value=
""
></td>
</tr>
<tr>
<td>
Category
</td>
<td>
<select
name=
"category"
>
{% for item in category %}
<option
value=
"{{item.id}}"
>
{{ item.category_text }}
</option>
{% endfor %}
</select>
</tr>
<td></td>
<td><a
type=
"button"
href =
"/products/"
>
Back
</a></td>
<td><input
type=
"submit"
value=
"Save"
></td>
</tr>
</table>
</form>
</form>
</div>
</div>
\ No newline at end of file
prodcat/templates/prodcat/productEdit.html
0 → 100644
View file @
faa7ae9f
<div>
<form
method=
"post"
>
{% csrf_token %}
{{ form.as_p }}
<input
type=
"submit"
value=
"Save"
>
</form>
</div>
\ No newline at end of file
prodcat/templates/prodcat/productIndex.html
View file @
faa7ae9f
...
@@ -13,12 +13,17 @@
...
@@ -13,12 +13,17 @@
</tr>
</tr>
</thead>
</thead>
<tbody>
<tbody>
{% for item in
data
%}
{% for item in
products_list
%}
<tr>
<tr>
<td>
{{item.name}}
</td>
<td>
{{item.name}}
</td>
<td>
{{item.price}}
</td>
<td>
{{item.price}}
</td>
<td>
{{item.restock_date}}
</td>
<td>
{{item.restockdate}}
</td>
<td>
{{item.category_id}}
</td>
<td>
{{item.category.category_text}}
</td>
<td>
<a
type=
"button"
href=
"delete-product/{{item.id}}"
onclick=
"return confirm('Are you sure to delete this category ?');"
>
Delete
</a>
<a
type=
"button"
href=
"edit-product/{{item.id}}"
>
Edit
</a>
</td>
</tr>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</tbody>
...
...
prodcat/urls.py
View file @
faa7ae9f
...
@@ -3,9 +3,12 @@ from . import views
...
@@ -3,9 +3,12 @@ from . import views
app_name
=
'prodcat'
app_name
=
'prodcat'
urlpatterns
=
[
urlpatterns
=
[
path
(
''
,
views
.
productIndex
,
name
=
'productIndex'
),
path
(
''
,
views
.
ProductsListView
.
as_view
(),
name
=
'product-index'
),
path
(
'cat/'
,
views
.
categoryIndex
,
name
=
'categoryIndex'
),
path
(
'add-product/'
,
views
.
ProductsCreateView
.
as_view
(),
name
=
'product-create'
),
path
(
'add-product/'
,
views
.
storeProduct
,
name
=
'storeProduct'
),
path
(
'edit-product/<int:id>'
,
views
.
UpdateProductsView
.
as_view
(),
name
=
'product-destroy'
),
path
(
'delete-product/<int:id>'
,
views
.
destroyProduct
),
path
(
'cat/'
,
views
.
categoryIndex
),
path
(
'cat/add-category/'
,
views
.
storeCategory
,
name
=
'storeCategory'
),
path
(
'cat/add-category/'
,
views
.
storeCategory
,
name
=
'storeCategory'
),
path
(
'cat/delete-category/<int:id>'
,
views
.
destroyCategory
)
path
(
'cat/delete-category/<int:id>'
,
views
.
destroyCategory
)
,
]
]
\ No newline at end of file
prodcat/views.py
View file @
faa7ae9f
from
django.shortcuts
import
render
,
redirect
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
.models
import
Products
,
Category
from
.models
import
Products
,
Category
from
.forms
import
CategoryForm
,
ProductForm
from
.forms
import
CategoryForm
,
ProductForm
from
django.views.generic
import
ListView
,
FormView
,
UpdateView
from
django.views.generic.edit
import
CreateView
from
django.urls
import
reverse_lazy
# Create your views here.
# Create your views here.
def
categoryIndex
(
request
):
def
categoryIndex
(
request
):
result
=
Category
.
objects
.
all
()
data
=
Category
.
objects
.
all
()
context
=
{
context
=
{
'
data'
:
result
,
'
category_list'
:
data
}
}
return
render
(
request
,
'prodcat/categoryIndex.html'
,
context
)
return
render
(
request
,
'prodcat/categoryIndex.html'
,
context
)
def
destroyCategory
(
request
,
id
):
def
destroyCategory
(
request
,
id
):
data
=
Category
.
objects
.
get
(
id
=
id
)
data
=
Category
.
objects
.
get
(
id
=
id
)
...
@@ -25,24 +28,32 @@ def storeCategory(request):
...
@@ -25,24 +28,32 @@ def storeCategory(request):
return
render
(
request
,
'prodcat/categoryAdd.html'
,
{
'form'
:
form
})
return
render
(
request
,
'prodcat/categoryAdd.html'
,
{
'form'
:
form
})
def
productIndex
(
request
):
class
ProductsListView
(
ListView
):
result
=
Products
.
objects
.
all
()
template_name
=
'prodcat/productIndex.html'
context
=
{
model
=
Products
'data'
:
result
,
def
get_context_data
(
self
,
**
kwargs
):
}
context
=
super
()
.
get_context_data
(
**
kwargs
)
return
render
(
request
,
'prodcat/productIndex.html'
,
context
)
context
[
'products_list'
]
=
Products
.
objects
.
all
()
return
context
def
storeProduct
(
request
):
class
ProductsCreateView
(
FormView
):
form
=
ProductForm
(
request
.
POST
or
None
,
request
.
FILES
or
None
)
form_class
=
ProductForm
if
request
.
method
==
'POST'
:
template_name
=
'prodcat/productAdd.html'
if
form
.
is_valid
():
success_url
=
reverse_lazy
(
'prodcat:product-index'
)
print
(
form
)
def
form_valid
(
self
,
form
):
form
.
save
()
form
.
save
()
return
redirect
(
'/products/'
)
return
super
()
.
form_valid
(
form
)
pass
cat
=
Category
.
objects
.
all
()
class
UpdateProductsView
(
UpdateView
):
context
=
{
model
=
Products
'category'
:
cat
,
form_class
=
ProductForm
'form'
:
form
template_name
=
'prodcat/productEdit.html'
}
success_url
=
reverse_lazy
(
'prodcat:product-index'
)
return
render
(
request
,
'prodcat/productAdd.html'
,
context
)
def
get_object
(
self
,
*
args
,
**
kwargs
):
\ No newline at end of file
product
=
get_object_or_404
(
Products
,
pk
=
self
.
kwargs
[
'id'
])
return
product
def
destroyProduct
(
request
,
id
):
data
=
Products
.
objects
.
get
(
id
=
id
)
data
.
delete
()
return
redirect
(
'/products/'
)
\ 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