[英]Django does not update with UpdateView
I have this view to show the previously populated data, which was only populated in the admin panel: 我有此视图来显示以前填充的数据,这些数据仅在管理面板中填充:
from .models import (Token,
Sell,
LogisticCost,
IncomeCost,
FinalPayment,
CustomerServiceCost,
Fatura)
def product_list(request):
context = {'product_list': ProductList.objects.filter(client=request.user.id).all(),
'data_payment': Fatura.objects.all()}
return render(request, 'Clientes/list_products.html', context)
This is the view to update these values: 这是更新这些值的视图:
class UpdateProduct(UpdateView):
model = ProductList
context_object_name = 'product_list'
template_name = 'Clientes/update_product.html'
fields = ['name', 'description', 'cost_price', 'sell_price', 'ncm']
My form in the update page is: 我在更新页面中的表单是:
<form method="post" action="{% url 'client:product_list' %}">
{% csrf_token %}
{{ form }}
<button class="btn btn-inverse" type="submit">Atualizar</button>
</form>
My update page is working as expected, showing every value related to the selected object, but when I submit the form the model has not changed. 我的更新页面按预期工作,显示了与所选对象相关的每个值,但是当我提交表单时,模型没有改变。 What is happening to not update the values?
发生什么事而不更新值?
You are submitting the form to the product_list
view - you should submit it to the update view instead. 您正在将表单提交到
product_list
视图-您应该将其提交到更新视图。
Depending on your URLs, the form should look something like: 根据您的URL,该表单应类似于:
<form method="post" action="{% url 'client:update_product' product_list.pk %}">
After all, I removed the action from the form and I added the variable success_url = reverse_lazy('client:product_list')
to my UpdateView. 毕竟,我从表单中删除了操作,并将变量
success_url = reverse_lazy('client:product_list')
到了UpdateView中。 That solved the problem. 那解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.