为什么我会立即获得HTTP 200的HTTP 200?我的印象是连续两次调用Ajax函数。我希望创建的对象实际上是通过......创建的。
我的ajax函数在表单提交时调用:
$(document).on('submit','#like_form_reading_list{{ article.pk }}', function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: '{% url "userprofile:like" %}',
data: {
journal: '{{ journal.pk }}',
section: '{{ section.pk }}',
article: '{{ article.pk }}',
csrfmiddlewaretoken: '{{ csrf_token }}'
},
success: function showAlertLike() {
$("#success_like").show().delay(2000).fadeOut();
},
error: function showAlertPin() {
$("#error_like").show().delay(2000).fadeOut();
}
});
});
在我看来:
class LikeView(generic.FormView):
"""
A view that creates a LikedArticles object when a user clicks on the like badge under each article displayed.
"""
def post(self, request):
if request.method == 'POST':
user = request.user
journal_pk = request.POST.get('journal')
section_pk = request.POST.get('section')
article_pk = request.POST.get('article')
journal = Journal.objects.get(pk=journal_pk)
section = Section.objects.get(pk=section_pk)
article = Article.objects.get(pk=article_pk)
print('IN LIKE VIEW ', user, journal_pk, section_pk, article_pk)
LikedArticles.objects.create(
user=user,
journal=journal,
section=section,
article=article
)
return HttpResponse('')
在我的终端:
内部服务器错误:/ like /
追踪(最近一次呼叫最后一次):
[某些错误消息]
“/ Users / benoitkoenig / Desktop / projet_web / harrispierce_django / hpdjango / lib / python2.7 / site-packages / django / db / models / fields / init .py”,第966行,在get_prep_value中 return int(value)
ValueError:基数为10的int()的无效文字:''
('IN LIKE VIEW',在0x107eb5c80>>,u'1',u'2',u'2390')
[31 / Oct / 2017 18:59:58]“POST / like / HTTP / 1.1”200 0
[31 / Oct / 2017 18:59:59]“POST / like / HTTP / 1.1”500 18107