Best way to trigger javascript function when returning a page from django view

110 views
Skip to first unread message

Martin Torre Castro

unread,
Sep 19, 2015, 4:09:35 AM9/19/15
to Django users
Hello,

I have a django project where I make requests to the server views both by AJAX and the usual  simple URL requests to Django.

The thing is that I have made one javascript function for storing the forms contents in the HTML5 webstorage and I want to clear the stored data after coming from a succesful operation, beacuse the flow is:

1. You get to the form

2. You enter the data into the form

3. Press submit
   a. If there is a validation error, we come back to 3 and the javascript function fills the same data entered into the form (this is already done and is awesome and comfortable for the user)

4. If all data was valid and OK, after returning from the python/django view I want to trigger the javascript function that clears the stored data, because is saved and we don't need it anymore because it's already saved. This is my problem


When returning to an AJAX I can simply use the success function in jQuery.get() o jQuery.ajax(), but when returning from a regular view I can't think of any strategy other than passing the flow to a success page, in which I trigger the function with $(document).ready(). 

I'm looking for the best alternative, because it would be better not forcing me to pass through a specific page. Any ideas?


Thank you in advance.

Gergely Polonkai

unread,
Sep 19, 2015, 7:04:36 AM9/19/15
to Django users

Hello,

I can't see the use case here. If you are submitting the form with AJAX, why would you store it? Or, if I misunderstood you and you submit in the non-AJAX way, Django will send give you back your filled form with the validation errors.

Best,
Gergely

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bffd59a7-c30f-4092-8160-30ef7e472b27%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Martín Torre Castro

unread,
Sep 19, 2015, 3:02:40 PM9/19/15
to django...@googlegroups.com

Maybe I should have made the context more clear.

First of all, we're not using ModelForms but just Django Forms.

It's true that when it's not filled correctly, the form returns the validation but we have cases in which the data in the fields is not returned when the form operation is not successful.

In those cases we copy the sent data in python and return that sent data, filling the form with it, but we have achieved a better, more generic way by JavaScript.

When the form operation is successful, we need the data no more, because is already saved in database, so we want to clear it from HTML5 webStorage by JavaScript.

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/eZJ-mQgp9w0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Luis Zárate

unread,
Sep 19, 2015, 9:34:32 PM9/19/15
to django...@googlegroups.com


In the success method do something like

$('myform').reset ();
I don't understand well but if you have cache clean it too.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKijOky93WY-R2ofYZc3qLr8n%3DSo0Z2u-VDd1gdETX_m-Wamww%40mail.gmail.com.

> For more options, visit https://groups.google.com/d/optout.
>

--
"La utopía sirve para caminar" Fernando Birri



Vijay Khemlani

unread,
Sep 20, 2015, 10:12:35 PM9/20/15
to django...@googlegroups.com
"It's true that when it's not filled correctly, the form returns the validation but we have cases in which the data in the fields is not returned when the form operation is not successful."

What cases? Dynamic fields or something like that? Django forms are supposed to pre-fill all the fields when there is a validation error.

Martín Torre Castro

unread,
Sep 21, 2015, 5:13:05 PM9/21/15
to django...@googlegroups.com
I'm looking soon which cases don't return the form filled with data, but I've just tested and we have one that validates, but doesn't return the data fields filled.

Maybe we're doing something wrong. I'm investigating.

Reply all
Reply to author
Forward
0 new messages