Passing an ID through a Modal to delete a user in a View

429 views
Skip to first unread message

Mauro Caresimo

unread,
Nov 5, 2018, 6:17:49 AM11/5/18
to Django users
Hello Im having some issues trying to pass an id through a modal. Basically I want to pass an id through a modal, via urls.py and to hit a function in the views to delete the user.
This is my modal:

Much appreciated for any help. Fairly new to Django. Really hope someone can come to my rescue :-)


<!-- BEGIN CONFIRM MODAL -->
<div class="modal fade" id="confirmModal" tabindex="-1" role="dialog">
   <div class="modal-dialog" role="document">
       <div class="modal-content">
           <div class="modal-header">
               <h3 class="modal-title">{% trans 'Delete My Account' %}</h3>
               <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                   <span aria-hidden="true">×</span>
               </button>
           </div>
           <div class="modal-body">
               {% trans 'Are you sure you want to delete your account?' %}
           </div>
           <div class="modal-footer">
               <div class="left-side">
                   <button type="button" class="btn btn-raised btn-default"
                           data-dismiss="modal">{% trans 'Cancel' %}</button>
               </div>
               <div class="divider"></div>
                <div class="right-side">
                   <a href="{% url 'deleteme'  request.user.id  %}" id="delete-account"
                      class="btn btn-raised btn-danger">{% trans 'Yes, delete me' %}</a>
               </div>
           </div>
       </div>
   </div>
</div>
<!-- END CONFIRM MODAL -->


{% block bottomjs %}
<script>
$(function () {
   $('#delete-account').on('click', function () {
       $('#ms-preload, #status').show();
   });
});
</script>
{% endblock %}



URLS>PY

from django.urls import path

from .views import subscriptions, subscribe, unsubscribe,deleteme, get_discounted_value, plan_details, my_subscription, manage_account


urlpatterns = [
   path('my-subscription/', my_subscription, name='my_subscription'),
   path('manage-account/', manage_account, name='manage_account'),
   path('subscriptions/', subscriptions, name='subscriptions'),
   path('subscribe/', subscribe, name='subscribe'),
   path('unsubscribe/', unsubscribe, name='unsubscribe'),
   path('deleteme/<int:id>/', deleteme, name='deleteme'),
   path('discounted-value/', get_discounted_value, name='discounted-value'),
   path('plan-details/', plan_details, name='plan-details'),
   # re_path('discounted-value/(?P<coupon_code>([^/]+))/(?P<original_value>([0-9.]+))/',
   #         get_discounted_value, name='assessment_question'),
]


VIEWS
@csrf_exempt
def deleteme(request):
   request.user.deleteme(request.POST)
   return redirect('free_dashboard_index')

Daniel Roseman

unread,
Nov 6, 2018, 5:30:53 AM11/6/18
to Django users
Your problem doesn't seem to have anything to do with modals or deleting. You should have posted the error you were getting, but presumably you have a TypeError because Django is trying to pass the id parameter from the URL to the view, but the view is not expecting it. Your view is, however, trying to do something with request.POST, but you don't actually send a POST request so even if you got that far it wouldn't work.

I think you are confusing a few things here. Your modal should actually submit a POST request, presumably via a form:

                <div class="right-side">
                   <form action="{% url 'deleteme'  %}">
                      <button id="delete-account" class="btn btn-raised btn-danger">{% trans 'Yes, delete me' %}</a>
               </div>

We also need to remove the ID from the URL:

    path('deleteme', deleteme, name='deleteme'),

Note that there is no point submitting the user ID  either in the URL or the POST, since it is already available via the request. So I don't know what the `deleteme()` method does, but again since it seems to be a method on the User model it shouldn't need to accept any parameters:

    request.user.deleteme()
--
DR.
Reply all
Reply to author
Forward
0 new messages