18 – HOW TO UPDATE DATA IN DJANGO – INVOICE MANAGEMENT SYSTEM

Spread the love

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>

Spread the love

About the author

arbadjie

Hi, I'm Abdourahman Badjie, an aspiring developer with obsession for anything coding and networking. This blog is dedicated to helping people learn to develop web applications using django framework.

View all posts

4 Comments

Leave a Reply to Tam Cancel reply

Your email address will not be published. Required fields are marked *