sending an email manually in Django admin panel, and show a successful alert instead of redirect

this my

from django.contrib import messages
from django.core.mail import send_mail
from django.shortcuts import redirect
from django.http import HttpResponse

def sendmail(request):
    if request.method == 'GET':
        send_mail('this is subject',
                'Hello alex',
                " <>",
    return HttpResponse('ok')

this my

from django.urls import path
from . import views

urlpatterns = [
    path('/redirect/' , views.sendmail, name='sendmail'),

this my

urlpatterns = [
    path('', include('register.urls')),

this is my change_form.html that override admin templates:

{% extends "admin/change_form.html" %}
{% load i18n %}

{% block submit_buttons_bottom %}
     <a href="{% url 'sendmail'%}"> <input type="button" value="Click" name="mybtn" /></a>

    {% if messages %}
        <ul class="messagelist">{% for message in messages %}<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>{% endfor %}</ul>
    {% endif %}

    {{ block.super }}
{% endblock %}

and this my app image:

my app

when I click on the button the email was sent.

look at this

but, I want when the email was sent, just show an alert that email sent successfully.

my problem is, I don’t want to redirect to another page. what should I do?

The short version is that you’ve got basically two choices.

  1. You can set the redirect url to this same page.

  2. You use JavaScript/AJAX to submit the request.

Any html form submission is going to result in a full page refresh. JavaScript is your only way around that.

1 Like

I use this tutorial to solve my problem :


Ok. I’m not seeing where the code on that page does what you said you were looking for above, but if it’s doing what you need it to do, great!