Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

655
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda