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 %}
  <tr class="{% cycle 'odd' 'even' %}">
    <td>{{ report.Name }}</td>
    <td>{{report.ReportType.name}}</td>
    <td><a class="rpt_h_{{ report.id }}"
       href="/generate/html/{{report.id}}/"
       target="_blank">html</a></td>
    <td><a class="rpt_c_{{ report.id }}"
       href="/generate/csv/{{report.id}}/">csv</a></td>
    <td><a class="rpt_p_{{ report.id }}"
       href="/generate/pdf/{{report.id}}/"
       target="_blank">pdf</a></td>
    <td align="right">{% if report.RunTime %}{{ report.RunTime|floatformat:2
}}{% endif %}</td>
    <td><a href="/reports/edit/summary/{{report.id}}">Edit</a></td>
    <td><a class="jqConfirm" href="/reports/delete/{{report.id
}}/">Delete</a></td>
    <td><span id="runtime_{{ report.id }}">{{ report.ManualRunTS|date:"Y-m-d
H:i" }}</span></td>
    <td>{% if report.NextRunDate %}{{ report.NextRunDate }}{% endif %}</td>
  </tr>
{% endfor %}

Report NNN's runtime is displayed as the content of a <span
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
<margierogin...@yahoo.com>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: <p>hello
> world</p>.
>
> Does my views.py function just return it like this?
>
>    return HttpResponse("<p>hello world</p>")
>
> 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:
>
>            <input type="submit" onclick="$(.post({% get_my_url %}, $
> ('id_comment').val(), function(data) { alert(data);});  return false;
> " />
>
> I find that I never hit my callback function (the alert(data)).
> Instead the browser just replaces my with a page containing
>
> <html><head><body>hello</body></html>
>
> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to