feat: edit product

This commit is contained in:
2024-12-30 10:46:16 -03:00
parent 9cc0c72f10
commit c9f7b33fde
7 changed files with 66 additions and 12 deletions

View File

@@ -20,7 +20,7 @@ Produtos
<body>
<div class="grid-container">
<div ><input type="text"></div>
<button id="openModal">Adicionar Novo Produto</button>
<button onclick="openModal()" id="openModal">Adicionar Novo Produto</button>
<table id="list-products">
<tr>
@@ -34,13 +34,13 @@ Produtos
{% for product in products %}
<tr>
<td>{{product.name}}</td>
<td>R$ {{product.price}}</td>
<td>{{product.quantity}}</td>
<td>{{product.category.name}}</td>
<td id="name-{{product.id}}" >{{product.name}}</td>
<td id="price-{{product.id}}" >R$ {{product.price}}</td>
<td id="quantity-{{product.id}}" >{{product.quantity}}</td>
<td id="category-{{product.id}}" >{{product.category.name}}</td>
<td>
<div class="grid-buttons">
<button>📝 Editar</button>
<button onclick="editProduct({{product.id}})" >📝 Editar</button>
<form action="{% url 'onOffproduct' %}" method="post">
{% csrf_token %}
@@ -72,15 +72,16 @@ Produtos
<dialog id='Modal-create-product' >
<article>
<article >
<form action="{% url 'create_product' %}" id="productForm" method="post" >
{% csrf_token %}
<h2>Cadastro de Produto</h2>
<input type="text" id="productId" name="productId" required hidden ><br>
<input type="text" id="productName" name="name" required placeholder="Nome"><br>
<input type="number" step="0.01" id="productPrice" name="price" required placeholder="Preço"><br>
<input type="number" step="1" id="productqtd" name="qtd" placeholder="Quantidade"><br>
<select name="select-categorie" >
<select id="select-categorie" name="select-categorie" >
{% for categorie in categories %}
<option value="{{categorie.id}}">{{categorie.name}}</option>
@@ -88,9 +89,11 @@ Produtos
</select>
<textarea id="productDescription" name="description" rows="4" placeholder="Descrição"></textarea><br>
<button type="submit">Salvar</button>
<button type="button" onclick="closeModal()" style="background-color:red;">Fechar</button>
</form>
<footer class="grid-buttons">
<button type="submit">Salvar</button>
<button type="button" onclick="closeModal()" style="background-color:red;">Fechar</button>
</footer>
</form>
</article>
</dialog>

View File

@@ -6,5 +6,6 @@ urlpatterns = [
path('', views.products, name='products'),
path('create_product', views.createProduct, name='create_product'),
path('onOffproduct', views.onOffProduct, name='onOffproduct'),
path('editProduct/<int:productId>/', views.editProduct, name='editProduct'),
]

View File

@@ -27,4 +27,16 @@ def onOffProduct(request):
product = Product.objects.get(id=product_id)
product.active = not product.active
product.save()
return redirect('products')
def editProduct(request, productId):
# id = request.POST.get('productId')
product_id = productId
product = Product.objects.get(id=product_id)
product.name = request.POST.get('name')
product.description = request.POST.get('description')
product.price = request.POST.get('price')
product.quantity = request.POST.get('qtd')
product.category = Categories.objects.get(id = int(request.POST.get('select-categorie')))
product.save()
return redirect('products')