weixin_33716941 2018-11-04 15:03 采纳率: 0%
浏览 23

Django和Ajax函数

I'm trying to run a jquery function on link click and return a modified html. This is the code:

Html

                      <li>
                        <a runat="server" href="javascript:void(0);" onclick= "myFunction( '{{ playlist.name }}')" >{{ playlist.name }}</a> 
                      </li>

Javascript

$(document).ready(function() {
});

var myFunction = function(playlist){
    console.log(playlist);

    $.ajax({
        url: '/ajax/select_playlist/',
        data: {
            'playlist': playlist
        },
        dataType: 'json',
        success: function (data) {
            console.log('This is not executing');
            console.log(data);
            $('#to_replace').html(data);
        },
    });
}

Views.py:

def select_playlist(request):

    playlist = request.GET.get('playlist', None)
    selected_playlist = Playlist.objects.get( name  = playlist)
    print(playlist)
    print(selected_playlist)
    html = render_to_string('Play/playlist_detail_ajax.html', {'sel_playlist': selected_playlist})
    return HttpResponse(html)

It works fine right until the ajax function, i can't figure out what i'm doing wrong with that success function.

  • 写回答

0条回答 默认 最新

    报告相同问题?