Re: how to return an html snippet using ajax $.post()?

2009-08-21 Thread Margie

Yes, I have been learning and using jquery a lot lately.  I find it
really makes traversing around in the dom easy.  Also, I have found
some excellent plugins such as the cluetip pluging and datepicker
plugins.  All good stuff!

Margie

On Aug 20, 3:16 pm, Matthias Kestenholz
 wrote:
> On Thu, Aug 20, 2009 at 11:53 PM, Margie wrote:
>
> > Ah - thank you!  Yes, sorry, in the process of my debugging the issue
> > and trying to simplify it, I unintentionally introduced even more
> > errors, and then when posting it, even more!  But you somehow despite
> > that, managed to identify my real error, which was that I hadn't put
> > the single quotes around {% get_my_url %}.
>
> > I was clearly doing a spiral downward - thanks very much for your
> > save!
>
> No problem! I don't know if you know about it, but if you are doing
> lots of ajax with forms, you should take a look at the jquery form
> plugin[1]; I use it (nearly) all the time. (No disclaimer, it's not my
> project and I'm not affiliated with it in any way.)
>
> Matthias
>
> [1]:http://malsup.com/jquery/form/
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to return an html snippet using ajax $.post()?

2009-08-21 Thread Margie

Thanks for that example, Steve.  I think I have made it past my
problem, but it is always good to see what others are doing.  In your
example you have this:

 target.innerHTML = (eval(data));

Why do you use eval there? Isn't data just a string, something like
'2009-08-21 11:41'?

Margie

On Aug 20, 3:59 pm, Steve Holden  wrote:
> Margie:
>
> Here's some code I Use to send the current date-time from a Django view into
> a client-side form using AJAX (in this case jQuery, but I guess you can
> substitute your Javascript extensions of choice.
>
> In the template:
>
> {% for report in my_reports %}
>   
>     {{ report.Name }}
>     {{report.ReportType.name}}
>             href="/generate/html/{{report.id}}/"
>        target="_blank">html
>             href="/generate/csv/{{report.id}}/">csv
>             href="/generate/pdf/{{report.id}}/"
>        target="_blank">pdf
>     {% if report.RunTime %}{{ 
> report.RunTime|floatformat:2}}{% endif %}
>
>     Edit
>      href="/reports/delete/{{report.id}}/">Delete
>
>     {{ report.ManualRunTS|date:"Y-m-d
> H:i" }}
>     {% if report.NextRunDate %}{{ report.NextRunDate }}{% endif %}
>   
> {% endfor %}
>
> Report NNN's runtime is displayed as the content of a  id="runtime_NNN"/> element.
>
> The following jQuery code associates a click-processor with each of the
> links:
>
>   $("a[class^='rpt_']").click(function(){
>     target = $(this).parent().parent().children().eq(8).children("span")[0];
>     $.get("/reports/json/timestamp/", function(data) {
>     target.innerHTML = (eval(data));
>     });
>   });
>
> When a link is clicked the jQuery expression assigns to target the span
> element whose
> content must be updated (to correctly show the new "last run time"). It then
> uses the
> jQuery $.get function to receive the (JSON) output of a call to the server
> for its
> current timestamp. This request is dispatched in the usual Django way,
> connecting the
> JavaScript to the following method:
>
> def timestamp(request,):
>     rdata = datetime.datetime.today().strftime("%Y-%m-%d %H:%M")
>     json = simplejson.dumps(rdata)
>     return HttpResponse(json, mimetype="application/json")
>
> The JavaScript dutifully evaluates the returned JSON [phobic security risk:
> it would be
> much better to use a JSON library for this] and replaces any current content
> content
> of the target span.
>
> Hope this helps.
>
> regards
>  Steve
>
> On Thu, Aug 20, 2009 at 5:34 PM, Margie Roginski
> wrote:
>
>
>
>
>
> > Could someone give me a hand with a very simple ajax problem?  I want
> > to post some data and have the server just return a small snippet of
> > html, which I then want to insert into my dom at a particular id.
>
> > Let's say the html snippet to be returned is just a string: hello
> > world.
>
> > Does my views.py function just return it like this?
>
> >    return HttpResponse("hello world")
>
> > I have some jquery on client side that is just trying trying to have
> > the callback function throw the returned snippet up in an alert box,
> > like this:
>
> >            
>
> > I find that I never hit my callback function (the alert(data)).
> > Instead the browser just replaces my with a page containing
>
> > hello
>
> > Could someone give me a pointer as to what I'm doing wrong?
>
> > Margie
>
> --
> Steve Holden        +1 571 484 6266  +1 800 494 3119
> Holden Web LLC            http://www.holdenweb.com/
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to return an html snippet using ajax $.post()?

2009-08-20 Thread Steve Holden
Margie:

Here's some code I Use to send the current date-time from a Django view into
a client-side form using AJAX (in this case jQuery, but I guess you can
substitute your Javascript extensions of choice.

In the template:

{% for report in my_reports %}
  
{{ report.Name }}
{{report.ReportType.name}}
html
csv
pdf
{% if report.RunTime %}{{ report.RunTime|floatformat:2
}}{% endif %}
Edit
Delete
{{ report.ManualRunTS|date:"Y-m-d
H:i" }}
{% if report.NextRunDate %}{{ report.NextRunDate }}{% endif %}
  
{% endfor %}

Report NNN's runtime is displayed as the content of a  element.

The following jQuery code associates a click-processor with each of the
links:

  $("a[class^='rpt_']").click(function(){
target = $(this).parent().parent().children().eq(8).children("span")[0];
$.get("/reports/json/timestamp/", function(data) {
target.innerHTML = (eval(data));
});
  });

When a link is clicked the jQuery expression assigns to target the span
element whose
content must be updated (to correctly show the new "last run time"). It then
uses the
jQuery $.get function to receive the (JSON) output of a call to the server
for its
current timestamp. This request is dispatched in the usual Django way,
connecting the
JavaScript to the following method:

def timestamp(request,):
rdata = datetime.datetime.today().strftime("%Y-%m-%d %H:%M")
json = simplejson.dumps(rdata)
return HttpResponse(json, mimetype="application/json")

The JavaScript dutifully evaluates the returned JSON [phobic security risk:
it would be
much better to use a JSON library for this] and replaces any current content
content
of the target span.

Hope this helps.

regards
 Steve

On Thu, Aug 20, 2009 at 5:34 PM, Margie Roginski
wrote:

>
> Could someone give me a hand with a very simple ajax problem?  I want
> to post some data and have the server just return a small snippet of
> html, which I then want to insert into my dom at a particular id.
>
> Let's say the html snippet to be returned is just a string: hello
> world.
>
> Does my views.py function just return it like this?
>
>return HttpResponse("hello world")
>
> I have some jquery on client side that is just trying trying to have
> the callback function throw the returned snippet up in an alert box,
> like this:
>
>
>
> I find that I never hit my callback function (the alert(data)).
> Instead the browser just replaces my with a page containing
>
> hello
>
> Could someone give me a pointer as to what I'm doing wrong?
>
> Margie
>
> >
>


-- 
Steve Holden+1 571 484 6266  +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to return an html snippet using ajax $.post()?

2009-08-20 Thread Matthias Kestenholz

On Thu, Aug 20, 2009 at 11:53 PM, Margie wrote:
>
> Ah - thank you!  Yes, sorry, in the process of my debugging the issue
> and trying to simplify it, I unintentionally introduced even more
> errors, and then when posting it, even more!  But you somehow despite
> that, managed to identify my real error, which was that I hadn't put
> the single quotes around {% get_my_url %}.
>
> I was clearly doing a spiral downward - thanks very much for your
> save!
>

No problem! I don't know if you know about it, but if you are doing
lots of ajax with forms, you should take a look at the jquery form
plugin[1]; I use it (nearly) all the time. (No disclaimer, it's not my
project and I'm not affiliated with it in any way.)


Matthias

[1]: http://malsup.com/jquery/form/

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to return an html snippet using ajax $.post()?

2009-08-20 Thread Margie

Ah - thank you!  Yes, sorry, in the process of my debugging the issue
and trying to simplify it, I unintentionally introduced even more
errors, and then when posting it, even more!  But you somehow despite
that, managed to identify my real error, which was that I hadn't put
the single quotes around {% get_my_url %}.

I was clearly doing a spiral downward - thanks very much for your
save!

Margie


On Aug 20, 2:40 pm, Matthias Kestenholz
 wrote:
> On Thu, Aug 20, 2009 at 11:34 PM, Margie
>
>
>
> Roginski wrote:
>
> > Could someone give me a hand with a very simple ajax problem?  I want
> > to post some data and have the server just return a small snippet of
> > html, which I then want to insert into my dom at a particular id.
>
> > Let's say the html snippet to be returned is just a string: hello
> > world.
>
> > Does my views.py function just return it like this?
>
> >    return HttpResponse("hello world")
>
> > I have some jquery on client side that is just trying trying to have
> > the callback function throw the returned snippet up in an alert box,
> > like this:
>
> >            
>
> > I find that I never hit my callback function (the alert(data)).
> > Instead the browser just replaces my with a page containing
>
> > hello
>
> > Could someone give me a pointer as to what I'm doing wrong?
>
> I'd say you get a javascript error in your onclick handler, this does
> not look like a server error at all. You should probably add quotes
> around the {% get_my_url %} stuff; or does this template tag add the
> quotes itself?
>
> After reading the snippet for a second time, I've got to say it looks
> quite messed up. This won't work. Try something like that:
>
>  
>
> (Parentheses in $.post removed, # added in $('#id_comment'), added
> quotes around {% get_my_url %} )
>
> Matthias
>
> --
> FeinCMS Django CMS building toolkit:http://spinlock.ch/pub/feincms/
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to return an html snippet using ajax $.post()?

2009-08-20 Thread Matthias Kestenholz

On Thu, Aug 20, 2009 at 11:34 PM, Margie
Roginski wrote:
>
> Could someone give me a hand with a very simple ajax problem?  I want
> to post some data and have the server just return a small snippet of
> html, which I then want to insert into my dom at a particular id.
>
> Let's say the html snippet to be returned is just a string: hello
> world.
>
> Does my views.py function just return it like this?
>
>    return HttpResponse("hello world")
>
> I have some jquery on client side that is just trying trying to have
> the callback function throw the returned snippet up in an alert box,
> like this:
>
>            
>
> I find that I never hit my callback function (the alert(data)).
> Instead the browser just replaces my with a page containing
>
> hello
>
> Could someone give me a pointer as to what I'm doing wrong?
>

I'd say you get a javascript error in your onclick handler, this does
not look like a server error at all. You should probably add quotes
around the {% get_my_url %} stuff; or does this template tag add the
quotes itself?

After reading the snippet for a second time, I've got to say it looks
quite messed up. This won't work. Try something like that:

 

(Parentheses in $.post removed, # added in $('#id_comment'), added
quotes around {% get_my_url %} )


Matthias


-- 
FeinCMS Django CMS building toolkit: http://spinlock.ch/pub/feincms/

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



how to return an html snippet using ajax $.post()?

2009-08-20 Thread Margie Roginski

Could someone give me a hand with a very simple ajax problem?  I want
to post some data and have the server just return a small snippet of
html, which I then want to insert into my dom at a particular id.

Let's say the html snippet to be returned is just a string: hello
world.

Does my views.py function just return it like this?

return HttpResponse("hello world")

I have some jquery on client side that is just trying trying to have
the callback function throw the returned snippet up in an alert box,
like this:



I find that I never hit my callback function (the alert(data)).
Instead the browser just replaces my with a page containing

hello

Could someone give me a pointer as to what I'm doing wrong?

Margie

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---