Reputation: 21
I'm getting the message in the title for product_id, but I think I've used it correctly on both product_form() and on its path. Can someone help please?
I was gonna name the variable id, but before I could even test it pylint was having trouble with it, so I changed to product_id.
views.py
def product_form(request, product_id=0):
"""Formulário de Cadastro de Produtos"""
if request.method == "GET":
if product_id==0:
form = ProductForm()
else:
product = Product.objects.get(pk=product_id)
form = ProductForm(instance=product)
return render(request, "product_register/product_form.html", {'form': form})
else:
form = ProductForm(request.POST)
if form.is_valid():
form.save()
return redirect('/product')
urls.py
urlpatterns = [
path('', views.product_list, name='product_list'),
path('product-form/', views.product_form, name='product_insert'),
path('product-form/<int:product_id>/', views.product_list, name='product_update'),
path('category-form/', views.category_form, name='category_insert')
]
html
{% extends "product_register/base.html" %}
{% block content %}
<table class="table table-borderless">
<thead class="border-bottom font-weight-bold">
<tr>
<td> Nome </td>
<td> Descrição </td>
<td> Preço </td>
<td> Categoria </td>
<td></td>
</tr>
</thead>
<tbody>
{% for product in product_list %}
<tr>
<td> {{product.name}} </td>
<td> {{product.description}} </td>
<td> {{product.price}} </td>
<td>
{% for category in product.category.all %}
{{category.name}},
{% endfor %}
</td>
<td>
<a href="{% url 'product_update' product.id %}" class="btn text-secondary px-0">
<i class="far fa-edit fa-lg"></i>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
Upvotes: 0
Views: 733
Reputation: 21
you should chnge
path('product-form/<int:product_id>/', views.product_list, name='product_update'),
to
path('product-form/<int:id>/', views.product_list, name='product_update'),
Upvotes: 1