python - How do I send a JavaScript confirm response over Django to a script to perform operations based on the response? -
i'm very new django , i'm trying build simple to-do application. i'm able display to-do list on webpage using html tables, able delete it, added new row @ end of every entry called "delete"
href element, on click event
sending js function pops confirmation user asking if wants delete it. i'm storing response in variable called response
. now, question how send response across django able delete entry? have done far , not working. appreciated. thank you.
{% item in task %} <tr> <td>{{ item.task_name }}</td> <td>{{ item.task_priority }}</td> <td>{{ item.task_status }}</td> <td>{{ item.target_date }}</td> <td><a href="" onclick="return delete_function()">delete</a></td> <p id="delete"></p> </tr> {% endfor %} </table> <script> function delete_function() { var response = confirm("are sure want delete item?"); if (response == true){ response.post() } } </script>
in views page, have function handle post method:
def view_task(request): if request.method == 'get': context = dict() context['task'] = task.objects.all() return render(request, 'todo/view_tasks.html', context) if request.method == 'post': task_name = request.post['task_name'] task.objects.delete(task_name) task.objects.save()
what doing wrong? also, explain me answer when , should use post , or @ least direct me answered questions same topic?
i don't think way solve problem,
try this:
in template.html:
<form method="post" action=""> {% csrf_token %} <input type="submit" name="delete_amount" onclick="delete_me(event)" /> </form>
in view.py:
#just in case have other form if 'delete_amount' in request.post: # deleting query here print('deleting amount')
and in javascript.js:
<script> function delete_me(e) { if(!confirm('are sure want delete this?')){ //prevent sending request when user clicked 'cancel' e.preventdefault(); } } </script>
hope help!
Comments
Post a Comment