[CentralOH] Not quite python
Brian Costlow
brian.costlow at gmail.com
Wed Jul 27 01:49:35 CEST 2011
At the meeting Monday, I mentioned a jquery plug-in that simplified
returning data from an xmlhttprequest, that was useful in certain situations
with Django.
But I couldn't remember the name.
It's taconite:
http://jquery.malsup.com/taconite/
This is especially useful with Django (or similar template systems) when you
want to return some part of the page with an initial page load and then
update it later via ajax-y type calls.
You can isolate the changeable bit of the page in its own template file,
then include that in both a larger template for the html page, and a
taconite wrapper template. Here's some totally contrived code that shows
what I'm talking about:
content.xml:
<div id="mydata">
<ul>
{% for foo in foo_list %}
<li>{{ foo.name <http://athlete.name/> }}</li>
{% endfor %}
</ul>
</div>
Next, set up a template with taconite commands in it and include the content
template.
ajax.xml:
<replace select="#mydata">
{% include "content.xml" %}
</replace>
Next set up your full page template and include the content template
fullpage.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Taco Night Tricks</title>
</head>
<body>
{% include "content.xml" %}
</body>
</html>
now in your views, set up a proc that returns the data that will populate
content.xml's data.
def get_real_data(request):
...do stuff to return a dict to populate content.xml template
then set up a view fuction for both rendering the full page, AND rendering
the response to an AJAX call:
def full_page(request):
return render_to_response(fullpage.html, get_real_data(request))
def ajax_part(request):
return render(request, ajax.xml,
get_real_data(request),content_type="text/xml")
If the data set returned evolves, you mod content.xml and get_real_data(),
the other stuff says the same.
This works really well for simple stuff, but it gets tricky when things get
complicated. For example if I was clicking a button and updating tabular
data, (or doing it on a time loop) this would be great. But if I wanted the
table itself to be interactive, I'd probably just send json and use the
datagrid plugin.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20110726/a0ec05fc/attachment.html>
More information about the CentralOH
mailing list