Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks
          <2009年9月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          常用鏈接

          留言簿(14)

          隨筆分類

          隨筆檔案

          友情鏈接

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          Using Django templates with jQuery AJAX

          2009 January 6
          by Nizam

          I recently discovered a neat way of displaying data retrieved using jquery AJAX in concert with Django’s template engine. You can create a view in Django which simply uses the render_to_response shortcut function to render the results server-side and then just use jquery.load to dynamically fetch the results.

          Eventhough, returning some raw JSON data is much more efficient as far as bandwidth is concerned, method is a lot simpler.

          I have been using jquery for over a year now. I have found that its built-in DOM manipulation features are a bit limited, especially for manipulating tables (e.g., adding rows dynamically). This method is much cleaner than doing all that DOM manipulation.

          Here is all the jQuery code to handle the search and AJAX spinner display:

          01.$( document ).ready( function() {
          02.    $( '#searchSubmit' ).click( function() {
          03.        q = $( '#q' ).val();
          04.        $( '#results' ).html( '&nbsp;' ).load(
          05.                    '{% url demo_user_search %}?q=' + q );
          06.    });
          07.});
          08. 
          09.$( document ).ajaxStart( function() {
          10.    $( '#spinner' ).show();
          11.}).ajaxStop( function() {
          12.    $( '#spinner' ).hide();
          13.});

          Here is the Django view function that does the heavy lifting on the server-side:

          01.def ajax_user_search( request ):
          02.    if request.is_ajax():
          03.        q = request.GET.get( 'q' )
          04.        if q is not None:
          05.            results = User.objects.filter(
          06.                Q( first_name__contains = q ) |
          07.                Q( last_name__contains = q ) |
          08.                Q( username__contains = q ) ).order_by( 'username' )
          09. 
          10.            template = 'results.html'
          11.            data = {
          12.                'results': results,
          13.            }
          14.            return render_to_response( template, data,
          15.                context_instance = RequestContext( request ) )

          Here are some screenshots of the results:

          AJAX operation in progress

          AJAX operation in progress

          Returned results

          Returned results

          No results

          No results

          The sample Django project is included for your perusal and is released under the MIT license. I used the excellent Aptana Studio IDE when working on this demo so it can be imported straight into it as an Aptana project.

          Download ajax_user_list.zip

          posted on 2009-09-24 08:51 seal 閱讀(2142) 評論(0)  編輯  收藏 所屬分類: Python
          主站蜘蛛池模板: 乐安县| 安溪县| 志丹县| 织金县| 儋州市| 建瓯市| 文成县| 赤峰市| 宜黄县| 荣昌县| 辽源市| 靖宇县| 阳东县| 凤山市| 济宁市| 祁东县| 榕江县| 秦皇岛市| 平乐县| 崇阳县| 枣庄市| 商洛市| 汝南县| 会昌县| 安吉县| 遵化市| 遂宁市| 壤塘县| 日照市| 临猗县| 阿克陶县| 沂水县| 永善县| 卢龙县| 仁化县| 山东省| 普安县| 蒙自县| 武强县| 郓城县| 双峰县|