java script alert from view after sucessfull opreation

27 views
Skip to first unread message

Navnath Gadakh

unread,
Sep 17, 2012, 2:30:43 AM9/17/12
to django...@googlegroups.com
how to use java script alert from view after sucessfull opreation in view ifself?

Jirka Vejrazka

unread,
Sep 17, 2012, 3:28:59 AM9/17/12
to django...@googlegroups.com
> how to use java script alert from view after sucessfull opreation in view

Simplest way is to set some context variable (a.k.a. "flag") in the
view (e.g. "operation_successful = True") and have a piece of JS code
in your template that runs only when this flag is set, e.g:

{% if operation_successful %}
<script type="javascript">
alert("operation was successful")
</script>
{% endif %}

There is a lot more ways to do this, at some later point you will
probably want to read about AJAX calls.

HTH

Jirka

Navnath Gadakh

unread,
Sep 17, 2012, 8:24:03 AM9/17/12
to django...@googlegroups.com
but i used function HttpResponse() not  render_to _response()..... So  how to carry variable from view to template with httpresponse()

jondykeman

unread,
Sep 17, 2012, 9:57:55 AM9/17/12
to django...@googlegroups.com
You can feed variables back through HttpResponse() using json serializer.
eg.
from django.utils import simplejson

response = {'Name': Name, 'Email': activeUser.email}
json = simplejson.dumps(response)
return HttpResponse(json, mimetype="text/json")

You should really look into AJAX as suggested earlier.

JD
Reply all
Reply to author
Forward
0 new messages