我坚持使用我的项目的Raiting模型(问答网站,如stackoverflow)。我使用Tango和Django http://www.tangowithdjango.com/book17/chapters/ajax.html的这个guid来添加带有ajax的按钮。然后我决定为喜欢和解决方案创建特殊模型。我不知道,我怎么能做到。 首先,我尝试这样做
class Rating(models.Model):
user = models.ForeignKey(CustomUser)
answer = models.ForeignKey(Answer)
like = models.IntegerField(default=0)
resolve = models.IntegerField(default=0)
url(r'add_like/(?P<answer_pk>[0-9]+)/$', views.add_like, name='add_like'),
def add_like(request, answer_pk):
if request.POST:
ans_rait = Rating.objects.get(answer=answer_pk)
ans_rait.like += 1
ans_rait.save()
return redirect(reverse(get_question))
在我的问题页面中,我在循环中显示问题模型和答案
{% for answer in answers %}
<div class="container-fluid no-padding">
{{ answer.text }}
</div>
{% if user.is_authenticated %}
<form method = "post" action = "{% url 'add_like' %}">
<button class="btn btn-default" type="submit"> like | {{ answer.rating.like }}
</button>
</form>
{% endif %}
请至少理解在循环中添加的逻辑。我尝试过很多东西,但不明白怎么做
更新我尝试过用户&#39; Hybrid&#39;解决方案:
当我点击按钮时,我有例外:&#34;本地变量&#39; ans_rait&#39;在转让之前引用&#34;。
当我在浏览器控制台中发布js脚本然后单击时,我有错误:
&#34;未捕获的ReferenceError:未定义csrfToken&#34;
&#34; POST http://127.0.0.1:8000/questions/add_like 500(内部服务器错误)&#34;
我认为问题在于模型评级的创建,我尝试更新这样的视图代码:
def add_like(request):
if request.POST:
answer_pk = request.POST.get('answer_pk')
ans_author = Answer.objects.get(pk=answer_pk)
if Answer.objects.filter(answer=answer_pk) is None:
Rating.objects.create(user=ans_author.author,
answer=ans_author.id, like=1, resolve=0)
data = {'message': 'Success'}
else:
ans_rait = Answer.objects.get(answer=answer_pk)
ans_rait.like += 1
ans_rait.save()
data = {'message': 'Success'}
return JsonResponse(data)
url: url(r'add_like$', views.add_like, name='add_like'),
我有例外:&#34;答案匹配查询不存在&#34;
我认为,我在逻辑上感到困惑,现在似乎最好的解决方案是添加&#34;喜欢&#34;并且&#34;解决了#34;答案模型中的极点,然后通过AnswerModel中的喜欢和解决计算用户评级。现在我的模型看起来像这样:
class Answer(models.Model):
text = models.TextField()
date = models.DateTimeField(default=datetime.datetime.now)
author = models.ForeignKey(CustomUser)
question = models.ForeignKey(Question)
答案 0 :(得分:1)
好的,所以你想要'喜欢'而不使用AJAX? 这条线有些问题:
<form method = "post" action = "{% url 'add_like' %}">
首先,你不需要等号周围的所有空格。而且你必须明确这个投票的答案,你必须把它放在URL中。所以:
<form method="post" action={% url 'add_like' answer.id %}">
例如,这将生成网址:add_like/23/
,因此将更新id = 23的答案
https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#url
答案 1 :(得分:0)
<form method = "post" action = "{% url 'add_like' %}">
应该是:
<form class="_addLike" data-answer-pk="{{ answer.pk }}" method="post" action="{% url 'add_like' %}">
现在它已经为AJAX做好了准备(把它放在页脚中): $(&#39; ._ addLike&#39;)。on(&#39; submit&#39;,function(){ var $ this = $(this); var actionURL = $ this.attr(&#39; action&#39;); var answerPK = $ this.data(&#39; answer-pk&#39;); $就({ 键入:&#39; POST&#39;, url:actionURL, 数据:{&#39; answer_pk&#39;:answerPK,&#39; csrfMiddlewareToken&#39;:csrfToken}, dataType:&#39; text&#39;, 成功:function(){ 警报(&#39;成功&#39;); }, 错误:function(){ 警报(&#39;错误&#39); } }) });
最后:
from django.http import JsonResponse
def add_like(request):
if request.POST:
data = {'message': 'Success'}
answer_pk = request.POST.get('answer_pk')
try:
ans_rait = Rating.objects.get(answer=answer_pk)
except Rating.DoesNotExist:
data = {'message': 'Invalid rating obj'}
if ans_rait:
ans_rait.like += 1
ans_rait.save()
return JsonResponse(data)