<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
    主站蜘蛛池模板: 成年女人看片免费视频播放器| 国内永久免费crm系统z在线| 成人免费视频77777| 亚洲色大成网站www永久| 精品一区二区三区免费毛片爱| 亚洲精品在线观看视频| 久草视频在线免费| 亚洲成av人片在线看片| 最近中文字幕免费mv视频8| 亚洲人AV在线无码影院观看| 午夜小视频免费观看| 狠狠入ady亚洲精品| 亚洲精品国产精品乱码不卞 | 今天免费中文字幕视频| 亚洲成av人片在线观看无码不卡| 久久永久免费人妻精品| 亚洲欧洲尹人香蕉综合| 日韩成人免费aa在线看| 一级女人18片毛片免费视频| 亚洲AV无码国产精品色午友在线| 18女人水真多免费高清毛片| 亚洲综合一区国产精品| 免费jjzz在在线播放国产| 国产亚洲精品免费视频播放 | 国产成人亚洲精品| 全部免费国产潢色一级| 中文字幕无码毛片免费看| 亚洲欧洲日产v特级毛片| 国产精品视_精品国产免费| 久久国产美女免费观看精品| 亚洲视频国产视频| 全部免费毛片在线| 久爱免费观看在线网站| 亚洲区日韩精品中文字幕| 亚洲男人天堂2020| 久久九九兔免费精品6| 老司机福利在线免费观看| 亚洲视频免费一区| 又黄又爽的视频免费看| 91制片厂制作传媒免费版樱花 | 污视频网站在线免费看|