如何使用Django和Evercookie创建非常简单和匿名的投票系统?

2024-04-26 23:34:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为匿名用户创建一个基本的上下投票系统,不需要登录,这样任何访问者都可以推一次,然后输入投票。显然我们必须限制人们一票。经过一些研究,但没有使用djangoratings模块,我发现对于django,evercookie是实现这种能力的最有希望的方法。但是,我需要一点帮助来编写代码的一部分,该代码将当前对一个对象投的投票的evercookies与可能的传入代码进行比较。我想我还有很多其他的事情要解决。在

我的基本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

我的模板中有一个简单的按钮/表单:

^{pr2}$

我的观点是:

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

Tags: theto对象django代码inurlget
1条回答
网友
1楼 · 发布于 2024-04-26 23:34:40

如果问题仍然有效,您可能需要查看Django-Evercookie

根据您的问题:您可以在处理请求时拒绝重复投票(例如,表单验证-调用evercookie的get方法,如果它返回了什么-则将其放入隐藏字段),或者进行DB/模型级别的验证,在这种情况下这可能是一种过度杀戮。在

相关问题 更多 >