我试图为匿名用户创建一个基本的向上向下投票系统,不需要登录,这样任何访问者都可以推动一次,投票是输入的。显然,我们必须限制人们只能投一票。经过一些研究之后,我发现django的evercookie是最有希望实现这一功能的方法。然而,我需要一些帮助来编写代码的一部分,将当前在对象上投票的evercookie与可能的传入代码进行比较。我想我已经弄清楚了很多其他的事情。
我的基本django模型对象的核心是一个提交的URL,其中包含一个用于跟踪点赞的IntegerField:
class newlink(models.Model):
linktag = models.ForeignKey(‘pagename’) #the page the link belongs in
linkcomment = models.CharField(max_length=128) #comment to go along with post
postlinkdate = models.DateTimeField(auto_now_add=True) #submission datestamp
url = models.URLField(max_length = 1024)
linklikescounter = models.IntegerField(null=False, default=0) #this is what will be changed up or down per vote
# Do I need another field(s) in this model to store evercookie data? Or maybe a new "likevote" class that has a ForeignKey relationship to the newlink class?
def __unicode__(self):
return self.url我的模板中有这个简单的按钮/表单:
<form action="/{{pagename_param}}" method="post">
{% csrf_token %}
<input type="hidden" name="linkset_likeid" value="{{ linkset.id }}">
<input type="submit" class="btn" value="like" name="linklikebtn"/>
</form>我对此的看法是:
if (request.POST.get('linklikebtn')):
linkid = request.POST[‘linkset_likeid’] #retrieve the ID from the form
url = newlink.objects.get(id=commentid) #get an instance of the desired url
url.linklikescounter += 1 #increase the IntegerField by 1
url.save() #save to the db发布于 2015-02-09 01:11:46
如果这个问题仍然有效,你可以去Django-Evercookie上看看
根据您的问题:您可以在处理请求时拒绝重复投票(例如,表单验证-调用evercookie的get方法,如果它返回某些内容-将其放入隐藏字段中),或者进行DB /模型级别的验证,这在这种情况下可能是一种过度的做法。
https://stackoverflow.com/questions/27209474
复制相似问题