1. Create a form for updating data
class InvoiceUpdateForm(forms.ModelForm):
class Meta:
model = Invoice
fields = ['--']
2. Import the form created in step 1 then make a view to handle and process update request.
def update_invoice(request, pk):
queryset = Invoice.objects.get(id=pk)
form = InvoiceUpdateForm(instance=queryset)
if request.method == 'POST':
form = InvoiceUpdateForm(request.POST, instance=queryset)
if form.is_valid():
form.save()
return redirect('/list_invoice')
context = {
'form':form
}
return render(request, 'entry.html', context)
3. Create a URL/PATH to pass the ID of the object to be updated
path('update_invoice/<str:pk>/', views.update_invoice, name="update_invoice"),
4. In the items_list page, and an anchor linked to the update URL/PATH
<td><a href="{% url 'update_invoice' instance.id %}">{{instance.name}}</a></td>
keep it good work, may Allah SWT bless you
Ameen and thanks
Hi, I’m a newbie. First, thanks you for your tutorial. It’s very useful for me
In this part, when I updated URL/PATH, there was a problem:
NoReverseMatch at /list_invoice/
Reverse for ‘update_invoice’ not found. ‘update_invoice’ is not a valid view function or pattern name
Could you show me how to solve this problem?
Check if you have ‘update_invoice’ in your views.py file