<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

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

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

    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 閱讀(2138) 評論(0)  編輯  收藏 所屬分類: Python
    主站蜘蛛池模板: 最近免费中文字幕大全免费| 在线观看成人免费视频不卡| 精品亚洲成a人片在线观看| **真实毛片免费观看| 亚洲aⅴ无码专区在线观看| 亚洲精品无码午夜福利中文字幕| 99re6热视频精品免费观看| 亚洲av纯肉无码精品动漫| 亚洲日韩小电影在线观看| 在线免费观看色片| av电影在线免费看| 亚洲伊人精品综合在合线| 久久久久久A亚洲欧洲AV冫| 四虎成年永久免费网站| www在线观看播放免费视频日本| 亚洲国产电影在线观看| 亚洲国产av无码精品| 国产1024精品视频专区免费| 72pao国产成视频永久免费| 亚洲五月丁香综合视频| 亚洲中文字幕第一页在线| 手机在线毛片免费播放| 久久久久国产精品免费看| 美女黄频视频大全免费的| 亚洲国产成人91精品| 国产亚洲精品精华液| 国产片免费福利片永久| 国产成人精品免费午夜app| 国产免费人成视频尤勿视频| 亚洲一区二区观看播放| 91在线亚洲精品专区| 亚洲色无码一区二区三区| 免费jjzz在线播放国产| 妞干网免费视频观看| 亚洲免费闲人蜜桃| 日本免费一区二区久久人人澡| 成年网站免费入口在线观看 | 毛片免费vip会员在线看| 99久久精品国产免费| 久久免费视频一区| 日韩毛片一区视频免费|