Skip to content

Commit

Permalink
Merge pull request #6 from fathonidf/development
Browse files Browse the repository at this point in the history
selesai tutorial 4 (tapi mau belajar navbar pake css
  • Loading branch information
fathonidf authored Sep 27, 2023
2 parents 0d951d2 + 88e0b27 commit 7b61f72
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 7 deletions.
22 changes: 22 additions & 0 deletions main/templates/edit_product.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends 'base.html' %}

{% load static %}

{% block content %}

<h1>Edit Product</h1>

<form method="POST">
{% csrf_token %}
<table>
{{ form.as_table }}
<tr>
<td></td>
<td>
<input type="submit" value="Edit Product"/>
</td>
</tr>
</table>
</form>

{% endblock %}
44 changes: 38 additions & 6 deletions main/templates/main.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
{% extends 'base.html' %}

{% block content %}

<style>

</style>

<nav class="navbar navbar-expand-lg bg-body-tertiary" data-bs-theme="dark">
<div class="container-fluid">
<a class="navbar-brand" href="#" style="color: white;">Daffa</a>
<button class="navbar-toggler" type="button" >
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{% url 'main:logout' %}" style="color: white;">Logout</a>
</li>
</ul>
</div>
</div>
</nav>



<h1>Shopping List Page</h1>

<h5>Name:</h5>
Expand All @@ -25,6 +48,21 @@ <h5>Class:</h5>
<td>{{product.price}}</td>
<td>{{product.description}}</td>
<td>{{product.date_added}}</td>
<td>
<a href="{% url 'main:edit_product' product.pk %}">
<button>
Edit
</button>
</a>
</td>
<td>
<a href="{% url 'main:delete_product' product.pk %}">
<button>
Delete
</button>
</a>

</td>
</tr>
{% endfor %}
</table>
Expand All @@ -39,10 +77,4 @@ <h5>Sesi terakhir login: {{ last_login }}</h5>
</button>
</a>

<a href="{% url 'main:logout' %}">
<button>
Logout
</button>
</a>

{% endblock content %}
4 changes: 3 additions & 1 deletion main/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.urls import path
from main.views import show_main, create_product, show_xml, show_json, show_xml_by_id, show_json_by_id, register, login_user, logout_user
from main.views import show_main, create_product, show_xml, show_json, show_xml_by_id, show_json_by_id, register, login_user, logout_user, edit_product, delete_product

app_name = 'main'

Expand All @@ -8,6 +8,8 @@
path('login/', login_user, name='login'),
path('logout/', logout_user, name='logout'),
path('create-product', create_product, name='create_product'),
path('edit_product/<int:id>/', edit_product, name='edit_product'),
path('delete/<int:id>', delete_product, name='delete_product'),
path('xml/', show_xml, name='show_xml'),
path('json/', show_json, name='show_json'),
path('xml/<int:id>/', show_xml_by_id, name='show_xml_by_id'),
Expand Down
23 changes: 23 additions & 0 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ def create_product(request):
context = {'form': form}
return render(request, "create_product.html", context)

def edit_product(request, id):
# Get product berdasarkan ID
product = Product.objects.get(pk = id)

# Set product sebagai instance dari form
form = ProductForm(request.POST or None, instance=product)

if form.is_valid() and request.method == "POST":
# Simpan form dan kembali ke halaman awal
form.save()
return HttpResponseRedirect(reverse('main:show_main'))

context = {'form': form}
return render(request, "edit_product.html", context)

def delete_product(request, id):
# Get data berdasarkan ID
product = Product.objects.get(pk = id)
# Hapus data
product.delete()
# Kembali ke halaman awal
return HttpResponseRedirect(reverse('main:show_main'))

def register(request):
form = UserCreationForm()

Expand Down
7 changes: 7 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
/>
{% block meta %}
{% endblock meta %}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha384-KyZXEAg3QhqLMpG8r+J4jsl5c9zdLKaUk5Ae5f5b1bw6AUn5f5v8FZJoMxm6f5cH1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+" crossorigin="anonymous"></script>
</head>

<body>
Expand Down

0 comments on commit 7b61f72

Please sign in to comment.