我想实现投票功能。投票函数无法获取对象。投票.js应该没事的。你知道吗?似乎没有发送POST请求。谢谢。
这是错误:
Page not found (404)
Request Method: GET
Request URL: http://.../vote/
Raised by: book.views.vote
No Eintrag matches the given query.
中的代码段结果.html地址:
<a href="/vote/" id="eintrag-vote-{{ eintrag.id }}" class="vote">▲</a>
<p id="eintrag-title-{{ eintrag.id }}">{{ eintrag.title }}</p>
你知道吗型号.py地址:
class Eintrag(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
title = models.CharField(max_length=200)
points = models.IntegerField(default=1)
text = models.TextField()
你知道吗视图.py地址:
@login_required
def vote(request):
eintrag = get_object_or_404(Eintrag, id=request.POST.get('eintrag'))
eintrag.points += 1
eintrag.save()
return HttpResponse()
你知道吗网址.py地址:
url(r'^vote/$', views.vote, name='vote'),
以及投票.js地址:
$(document).ready(function() {
// using jQuery
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
function.vote(eintragID) {
$.ajax({
type: "POST",
url: "/vote/",
data: {
"eintrag": eintragID
},
success: function() {
$("#eintrag-vote-" + eintragID).hide();
$("#eintrag-title-" + eintragID).css({
"margin-left": "15px"
});
},
headers: {
'X-CSRFToken': csrftoken
}
});
return false;
}
$("a.vote").click(function() {
var eintragID = parseInt(this.id.split("-")[2]);
return vote(eintragID);
})
});
似乎您的请求方法不匹配。当你应该做一篇文章的时候,你正在做一篇文章。你知道吗
此行引发404,因为没有
request.POST
:我邀请您用
django.views.decorators.http.require_POST
来装饰这个视图,这样您得到的HTTP 405 Method not allowed error
比404更好地反映了方法问题我认为它来自于如何在JS中完成Ajax请求(参见http://api.jquery.com/jquery.ajax/)
你能试着改变一下吗?你知道吗
相关问题 更多 >
编程相关推荐