<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GrenadePod &#187; javascript</title>
	<atom:link href="http://www.grenadepod.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.grenadepod.com</link>
	<description>Dispersing the Seeds</description>
	<lastBuildDate>Mon, 22 Feb 2010 20:30:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Query data from Django site with jQuery</title>
		<link>http://www.grenadepod.com/2009/11/13/query-data-from-django-site-with-jquery/</link>
		<comments>http://www.grenadepod.com/2009/11/13/query-data-from-django-site-with-jquery/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 16:06:37 +0000</pubDate>
		<dc:creator>pulegium</dc:creator>
				<category><![CDATA[IT Technology]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.grenadepod.com/?p=331</guid>
		<description><![CDATA[Right, here&#8217;s what I needed to do: I have a list of IP addresses displayed on a web page and I want to show the status of every each of them. By &#8220;status&#8221; I mean just perform a simple ping and display OK next to the IP, or &#8216;no response&#8217; otherwise. Listing is a Django [...]


Related posts:<ol><li><a href='http://www.grenadepod.com/2009/11/09/how-to-use-generic-views-in-django/' rel='bookmark' title='Permanent Link: How to use generic views in Django'>How to use generic views in Django</a></li>
<li><a href='http://www.grenadepod.com/2009/11/18/developing-my-first-wordpress-plugin/' rel='bookmark' title='Permanent Link: Developing my first WordPress plugin'>Developing my first WordPress plugin</a></li>
<li><a href='http://www.grenadepod.com/2009/11/22/using-openid-for-authentication-in-django/' rel='bookmark' title='Permanent Link: Using OpenID for authentication in Django'>Using OpenID for authentication in Django</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p id="top" />Right, here&#8217;s what I needed to do: I have a list of IP addresses displayed on a web page and I want to show the status of every each of them. By &#8220;status&#8221; I mean just perform a simple ping and display OK next to the IP, or &#8216;no response&#8217; otherwise.</p>
<p>Listing is a Django application, so theoretically I could just go through the list of IPs, ping and add status to the list that gets passed to Django template. But nice it is not. It might take ages to query the whole list, and even if I spend time implementing multithreaded ping and ping them all concurrently it still going to take at leas couple of seconds to complete.</p>
<p>So obvious choice is to delay the ping action. I will serve the page with all IPs and other bits and pieces and then let my JavaScript to do the AJAX magic.</p>
<p>So, how do I do that?</p>
<p>Well, first I need a view that checks if the given IP responds to a ping and displays the result. To keep things simple, I&#8217;m going to return a string, which will be displayed on the list page:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> ping<span style="color: black;">&#40;</span>request, address=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> responding_to_ping<span style="color: black;">&#40;</span>address<span style="color: black;">&#41;</span>:
        msg = <span style="color: #483d8b;">&quot;Ping OK&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        msg = <span style="color: #483d8b;">&quot;No response&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span></pre></div></div>

<p>I then need to add URL pattern, so I can call the view:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^ping/(?P&lt;address&gt;<span style="color: #000099; font-weight: bold;">\d</span>{1,3}<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\d</span>{1,3}<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\d</span>{1,3}<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\d</span>{1,3})/$'</span>, views.<span style="color: black;">ping</span><span style="color: black;">&#41;</span>,</pre></div></div>

<p>Good, so now I can navigate to http://<hostname>/ping/<ip_address>/ and it&#8217;ll come back with either &#8220;Ping OK&#8221; or &#8220;No response&#8221; message.<br />
Right, now let&#8217;s add some JavaScript magic that&#8217;ll query the server and update my web page with details.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
    $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.address&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> curId <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            updateStatus<span style="color: #009900;">&#40;</span>curId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> updateStatus<span style="color: #009900;">&#40;</span>attrId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        address <span style="color: #339933;">=</span> attrId.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ip_'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        address <span style="color: #339933;">=</span> address.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/_/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/ping/'</span> <span style="color: #339933;">+</span> address<span style="color: #339933;">,</span>
            success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> attrId<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>So, what exactly is happening here?</p>
<ul>
<li><strong>Line 3:</strong> This waits for a document too load and call the callback function when ready to proceed, calback function defined in the lines below</li>
<li><strong>Lines 4-6:</strong>Goes through each element of the HTML document (my web page) that are of class &#8220;address&#8221;, extracts the element&#8217;s &#8216;id&#8217; attribute, and calls &#8216;updateStatus&#8217; function passing the &#8216;id&#8217; to it. So all elements that are going to be updated should look like: &lt;span class=&#8221;address&#8221; id=&#8221;ip_address&#8221; /&gt;</li>
<li><strong>Lines 11-12:</strong> All id&#8217;s are going to be in the following format: ip_1_2_3_4, so before I pass this to the URL, I need to strip the ip_ bit, and replace underscores with dots.</li>
<li><strong>Lines 13-17:</strong> This is where I perform AJAX call to the URL. The &#8216;success&#8217; bit is a call to the function that gets executed when the result is successfully retrieved. This function is pretty simple, it just replaces the contents of the tag with whatever it receives from the server</li>
</ul>
<p>And this is it, all that is left to do is list all IPs that need checking. Below is the raw HTML, but I trust you can be creative in writing a decent Django template to generate the page.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;ul&gt;
    &lt;li&gt;192.168.0.1 [Status: &lt;span class=&quot;address&quot; id=&quot;ip_192_168_0_1&quot;&gt;Unknown&lt;/span&gt; ]&lt;/li&gt;
    &lt;li&gt;192.168.0.2 [Status: &lt;span class=&quot;address&quot; id=&quot;ip_192_168_0_2&quot;&gt;Unknown&lt;/span&gt; ]&lt;/li&gt;
    &lt;li&gt;192.168.0.3 [Status: &lt;span class=&quot;address&quot; id=&quot;ip_192_168_0_3&quot;&gt;Unknown&lt;/span&gt; ]&lt;/li&gt;
    ...
&lt;/ul&gt;</pre></div></div>



<p>Related posts:<ol><li><a href='http://www.grenadepod.com/2009/11/09/how-to-use-generic-views-in-django/' rel='bookmark' title='Permanent Link: How to use generic views in Django'>How to use generic views in Django</a></li>
<li><a href='http://www.grenadepod.com/2009/11/18/developing-my-first-wordpress-plugin/' rel='bookmark' title='Permanent Link: Developing my first WordPress plugin'>Developing my first WordPress plugin</a></li>
<li><a href='http://www.grenadepod.com/2009/11/22/using-openid-for-authentication-in-django/' rel='bookmark' title='Permanent Link: Using OpenID for authentication in Django'>Using OpenID for authentication in Django</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.grenadepod.com/2009/11/13/query-data-from-django-site-with-jquery/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
