は私がPOSTクエリデータとAJAXを使用しています。空のQueryDictジャンゴ
しかし、ファイヤーバグは、POST 302
とaction=add
またはaction=remove
のパラメータを示します。だから私はなぜ<QueryDict: {}>
が空であるのか理解できません。助けてください。
PS。 GETを使うとうまくいきます。
テンプレートshow_event.html:
<form id="unfollow" {% if user not in event.users.all %}style="display:none;"{% endif %}>
<input type="hidden" value="{{ event.id }}" name="remove">
<button type="submit" class="btn btn-warning btn-block">{% trans "Remove from My events"%}</button>
</form>
<form id="follow" {% if user in event.users.all %}style="display:none;"{% endif %}>
<input type="hidden" value="{{ event.id }}" name="add">
<button type="submit" class="btn btn-primary btn-block">{% trans "Add to My events"%}</button>
</form>
$(document).on('submit','#unfollow', function(e){
e.preventDefault();
$.ajax({
type:"POST",
url:'/event/follow/{{ event.id }}/',
data: {'action':'remove'},
success:function(){
$('#unfollow').hide();
$('#follow').show();
}
})
});
$(document).on('submit','#follow', function(e){
e.preventDefault();
$.ajax({
type:"POST",
url:'/event/follow/{{ event.id }}/',
data: {'action':'add'},
success:function(){
$('#follow').hide();
$('#unfollow').show();
}
})
});
views.py:
def follow (request, event_id):
event = get_object_or_404(Event, id=event_id)
user = request.user
print request.POST
if request.method == 'POST':
print "post"
if request.POST['action'] == 'add':
print "add"
event.users.add(user)
event.save()
elif request.POST['action'] == 'remove':
print "remove"
event.users.remove(user)
event.save()
return HttpResponse('')
urls.py:
url(r'^event/follow/(?P<event_id>[0-9]+)/$', 'events.views.follow', name='follow')
「500」ステータスエラーはありますか?ビューは 'counter_id'をkwargとして受け取り、' event_id'を渡しています。これは同じ見解ですか? – v1k45
@ v1k45申し訳ありませんが私は間違ったビューを貼り付け、今私は私の質問を編集しました。 500エラーがありません – worm2d