我正在創建一個使用Django的論壇。我有很多工作,包括投票。我無法弄清楚的一件事是防止重複投票。我怎麼能得到這個工作?有沒有一種方法可以使用JS在HTML中只能發送一次的表單?或者我需要在視圖中執行一些特殊操作?這是我的模板代碼:Django投票系統:防止重複投票
{% for comment in comments %}
<div class="ui container segment">
<img class="ui avatar image" src="/{{ comment.by.userprofile.img.url }}"><b>{{ comment.by }}</b>
<p style="font-size: 20px">{{ comment.body }}</p>
<form action="" method="post">
{% csrf_token %}
<input type="submit" value="Thumbs up" class="ui small blue button">
<i class="thumbs up outline icon"></i>
<input type="hidden" value="{{ comment.id }}" name="comment">
</form>
<span>{{ comment.points }}</span>
</div>
{% endfor %}
和我的意見代碼:
elif request.method == 'POST':
print request.POST
if 'body' in request.POST.keys():
reply = ForumReply.objects.create(by=request.user, reply_to=post, body=request.POST['body'])
reply.save()
notification = Notification.objects.create(to=post.by, message='Your post "' + post.title + '" received a new reply')
notification.save()
if 'comment' in request.POST.keys():
comment = post.forumreply_set.filter(pk=request.POST['comment'])[0]
comment.points += 1
comment.save()
我的模型(每樂高Stormtroopr的要求)
class ForumReply(models.Model):
by = models.ForeignKey(User)
reply_to = models.ForeignKey(ForumPost)
body = models.TextField()
created = models.DateField(default=timezone.now())
points = models.IntegerField(default=0)
向我們展示您用來存儲投票的模型? –