Sometimes, we want to call a view function from template with Python Django.
In this article, we’ll look at how to call a view function from template with Python Django.
How to call a view function from template with Python Django?
To call a view function from template with Python Django, we can add a link to the URL of the view function.
For instance, in our template, we write
<a class="btn btn-primary" href="{% url 'delete_product'%}">Delete</a>
to add a link to the view with name delete_product
in admin_page.html.
Then in urls.py, we write
path('delete_product', views.delete_product, name='delete_product')]
to add the path to the delete_product
view.
Then in views.py, we write
def delete_product(request):
if request.method == "GET":
dest = Racket.objects.all()
dest.delete()
return render(request, "admin_page.html")
to add the delete_product
view.
Conclusion
To call a view function from template with Python Django, we can add a link to the URL of the view function.
2 replies on “How to call a view function from template with Python Django?”
what is Racket here…can I use this method in Django
Racket is a made up model class.
You should replace that with your own model class.