Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

653
Vistas
Display JavaScript alert after form submit - Django

Im trying to show an alert if the form submited is valid or not.
In the view, I have a hidden input which change the value if form is valid or not.

I have this JavaScript:

$( document ).ready(function() {
  if (document.registroForm.alert.value==1){
    swal("¡Bien hecho!", "El registro fue exitoso.", "success")
  }
  if (document.registroForm.alert.value==0){
    swal("¡Oops!", "Algo salió mal.", "error")
  }
});

The problem is that I get the alert every time I refresh the page.
And I just wanna do it when the user submit the form and the page is refreshed

Thanks.

The problem was solved.

What I did, is to set the variable "alert" to None on page load. which changes after form submit: (I modified this on my views.py)

views.py

def registroUsuario(request):
    if request.method == "POST":
        form = registroForm(request.POST or None)
        if form.is_valid():
            alert = 1
            instance = form.save(commit=False)
            instance.is_active = False
            instance.save()
        else:
            alert = 0
    else:
        alert = None
        form = registroForm()
    context = {
        "titulo": "Registrarse",
        "form": form,
        "alert": alert,
    }
    template = "micuenta/registro.html"
    return render(request, template, context)

and my .html:

<form method="POST" action=".">
[... some labels and inputs ...]
<input type="hidden" name="alert" value="{{alert}}" readonly>
</form>
<script>
$( document ).ready(function() {
  if (document.registroForm.alert.value==1){
    swal("¡Bien hecho!", "El registro fue exitoso.", "success")
  }
  if (document.registroForm.alert.value==0){
    swal("¡Oops!", "Algo salió mal.", "error")
  }
});
</script>

Thanks by the way.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I've used something like this in the past.

$( document ).ready(function() {
    $('#your-form').on('submit', function(e) {
        e.preventDefault();
          if (document.registroForm.alert.value==1){
                swal("¡Bien hecho!", "El registro fue exitoso.", "success");
                return true;

          } else if (document.registroForm.alert.value==0){
                swal("¡Oops!", "Algo salió mal.", "error")

          }
    });
});

However you accomplish it, I believe you'll need an event listener for the form submission and disable the post back by default. You can then perform your check for a valid submission.

over 4 years ago · Santiago Trujillo Denunciar

0

You can use a flag variable to control it. You pass the variabel by the URL.

In your button:

<button type="button" onclick="myFunction(1)">Submit</button>

In the script of this same page:

<script type="text/javascript">
   function myFunction(value){
        window.location = "page.html?myVariabel="+value;
   }
</script>

In the page you wanna check and display the mensseger you can use this:

<script type="text/javascript">
    var query = location.search.slice(1);
    var part = query.split('=');
    if(part[1] == 1){
        //Your code
    }
</script>
over 4 years ago · Santiago Trujillo Denunciar

0

What I did, is to set the variable "alert" to None on page load. which changes after form submit: (I modified this on my views.py)

views.py

def registroUsuario(request):
    if request.method == "POST":
        form = registroForm(request.POST or None)
        if form.is_valid():
            alert = 1
            instance = form.save(commit=False)
            instance.is_active = False
            instance.save()
        else:
            alert = 0
    else:
        alert = None
        form = registroForm()
    context = {
        "titulo": "Registrarse",
        "form": form,
        "alert": alert,
    }
    template = "micuenta/registro.html"
    return render(request, template, context)

and my .html:

<form method="POST" action=".">
[... some labels and inputs ...]
<input type="hidden" name="alert" value="{{alert}}" readonly>
</form>
<script>
$( document ).ready(function() {
  if (document.registroForm.alert.value==1){
    swal("¡Bien hecho!", "El registro fue exitoso.", "success")
  }
  if (document.registroForm.alert.value==0){
    swal("¡Oops!", "Algo salió mal.", "error")
  }
});
</script>

Thanks by the way.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda