我正在创建一个使用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)
向我们展示您用来存储投票的模型? –