简体   繁体   中英

Send Javascript Push Notification - AFTER Form POST and URL redirection

Use case (Django Project):

I want to log in on my login page -> Therefore I use this form in html (login.html):

<form class="loginform" action="" method="POST">
    {% csrf_token %}
    <div class="loginform">
        {{form.as_p}}
    </div>
    <br>
        <input type="submit" class="btn btn-success loginbtn" value="Login">
</form>

In case of successful log in I will redirect with DJANGO to this URL (localhost/welcome/)-> welcome.html:

LOGIN_REDIRECT_URL = "welcome"

I am able to send javascript notifications with alertify, but only when using simple things such as click on it or mouseover:

function notification(text) {
console.log(text)
alertify.success(text);
}

I tried it with onsubmit="javascript:notification("")" in the html form tag, however this will be displayed only BEFORE the URL redirection.

So my question is:

  • How is it possible to activate /send the push notification AFTER the URL redirection and in case of successfull POST/ Log in?

I really appreciate your help! Thank you a lot!!

Javascript is client-side. That means your alertify.success(text); will be executed client-side, once server already did his render process.

The authentication process is server-side. So you have to make your server make your alertify a part of the rendered content.

Knowing that, you just have to change your LOGIN_REDIRECT_URL content page to include in it a call to notification:

<!-- your html template comes here -->
<script type="text/javascript">
notification('You are now logged in');
</script>

Be remembered that the notification will then display each time the person visits this page. You may want either to redirect him again, or to introduce a variable in Django to avoid displaying it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM