Discussion:
Passing an ID through a Modal to delete a user in a View
Mauro Caresimo
2018-11-05 11:03:35 UTC
Permalink
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')
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b3aace83-d3f1-43a7-9f10-5a1a07442608%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Daniel Roseman
2018-11-06 10:30:53 UTC
Permalink
Post by Mauro Caresimo
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.
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
request.user.deleteme(request.POST)
return redirect('free_dashboard_index')
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.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a34c130f-d621-48d7-80ee-8ecd2b414dcf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...