Call Django On Html Button Click
I'm trying to make a basic sort function for a Django project, but I don't know how to call the sort function when I click the 'sort' button Django view: def sort_btn(request): if
Solution 1:
you need to wrap your <button> with <form> tag, as following snippet:
<form action='actionUrl' method='GET'>
<button type='submit'> sort me</button>
</form>
and in your urls.py module you should point the actionUrl with specific view from the views.py module as follow: 
from django.urlsimport path
from . import views
urlpatterns = [
    path(actionUrl, views.yourSort),
]
you should more read about request lifecycle on Django:
- user submit request
 - Django tries to get the first match between request's URL and routes defined in 
urls.py - the request is passed to the matched view
 - the view get executed, usually this some model processing
 - a response (template) is returned by the view as 
HttpResponse 
Solution 2:
I don't know about your program but my implementation is the full as it is that worked.
<button name="button"type="submit" onclick="location.href='{% url "actionUrl" %}'"> Send</button>
I am trying to call a function in views named "sendEmail"
path('actionUrl/', views.sendEmail,name='actionUrl')
And above one is the line which I included in urls file.
As I am new to Django, Please check if I am wrong.
Keep Coding!!
Post a Comment for "Call Django On Html Button Click"