我已经建立了一个“保存”按钮。用户单击“保存”按钮将记录保存到其集合中。这会将“保存”按钮文本更改为“已保存”。然后,用户可以单击“已保存”以取消保存记录。
用户可以成功保存记录,ajax会根据需要更新html,方法是将Thread.CurrentThread.CurrentCulture = New CultureInfo("en-Us")
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-Us")
更改为id="likes"
。
问题是ajax没有提取id =“unsave”,因此没有路由到id="unsave"
网址,而是路由到unsave_release
网址。
网址示例
http://127.0.0.1:8000/save_release/?release_id=585197&num_saves=14 http://127.0.0.1:8000/unsave_release/?release_id=585197&num_saves=14
用户进行初始保存后,是否需要以某种方式重置ajax?
的 Models.py
save_release
ajax.js
@login_required
def save_release(request):
release_id = None
num_saves = None
username = request.user.username
if request.method == 'GET':
release_id = request.GET['release_id']
num_saves = request.GET['num_saves']
if release_id:
insert = ChartsExtended(release_id=release_id,artist=username,url='Like',date='date',type='U',source='net')
insert.save()
num_saves = str(int(num_saves) + 1)
return HttpResponse(num_saves)
@login_required
def unsave_release(request):
release_id = None
num_saves = None
username = request.user.username
if request.method == 'GET':
release_id = request.GET['release_id']
num_saves = request.GET['num_saves']
if release_id:
insert = ChartsExtended.objects.get(release_id=release_id,artist=username)
insert.delete()
num_saves = str(int(num_saves) - 1)
return HttpResponse(num_saves)
release.html
$(document).ready(function() {
// JQuery code to be added in here.
$('#likes').click(function(){
var release_id;
var num_saves;
release_id = $(this).attr("data-releaseid");
num_saves = $(this).attr("data-numsaves")
$.get('/save_release/', {release_id: release_id,num_saves: num_saves},
function(data){
$('#like_count').html(data + " people saved this");
// $('#likes').css({"background-color":"#FF0000"});
$('#likes').toggleClass("btn-info");
$('#likes').text('Saved!')
$('#likes').attr('id','unsave');
});
});
//this unsaves a release from Release
$('#unsave').click(function(){
var release_id;
var num_saves;
release_id = $(this).attr("data-releaseid");
num_saves = $(this).attr("data-numsaves")
$.get('/unsave_release/', {release_id: release_id,num_saves: num_saves},
function(data){
$('#like_count').html(data + " people saved this");
// $('#likes').css({"background-color":"#FF0000"});
$('#unsave').toggleClass("btn-success");
$('#unsave').text('Save')
$('#unsave').attr('id','likes');
});
});
在用户点击“保存”后转换为以下内容
<h3 id="like_count">{{dict_item.cnt}} people saved this</h3>
<p><button id="likes" data-releaseid="{{ dict_item.id }}" data-numsaves="{{dict_item.cnt}}" class="btn btn-xlarge btn-success release-follow-buttons" type="button">Save</button></p>
urls.py
<p><button id="unsave" data-releaseid="release_id" data-numsaves="num_saves" class="btn btn-xlarge btn-success release-follow-buttons btn-info" type="button">Saved!</button></p>