0

私は最近Djangoの学習を始めたので、まだ少し混乱しています。

誰かがリンクやチュートリアルに案内してくれたり、次のことを理解するのを手伝ってくれたりすると、本当にうれしいです.

- ユーザーが 1 日 1 回だけ投票できるようにする

これは私のmodels.pyからのものです

class YoungArtistShortlisted(models.Model):
    image = models.ImageField(upload_to=upload_file_path, blank=True, null=True)
    artist = models.CharField(max_length=200)
    age = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add=True, db_index=True)
    modified = models.DateTimeField(auto_now=True, db_index=True)
    location = models.CharField(max_length=3, choices=LOCATION_CHOICES)
    likes = models.IntegerField(default=0)

    def __unicode__(self):
        return self.artist

これは私のviews.pyです

def vote(request, youngartistshortlisted_id):
    p = get_object_or_404(YoungArtistShortlisted, pk=youngartistshortlisted_id)
    p.likes += 1
    p.save()

    return HttpResponseRedirect(reverse_lazy('youngartist:submission_vote', args=(p.id,)))

現在、ユーザーが Facebook でログインしたときにユーザーを自動的に作成するアプリを作成しています。投票を1日1回に制限する方法がまったくわからないので、Googleで何も見つからないように見えるので、助けていただければ幸いです. ありがとう!

私はDjango 1.8.2を使用しています

4

1 に答える 1

1

Just add a field to keep track of the last time the user voted.

Example,

last_vote_time = models.DateTimeField()

and in views.py, check if last_vote_time has a 24 hour difference from the current time.

This should help. Tell me if you need some code. But, I think you'll be able to do it.

于 2015-08-11T11:49:35.173 に答える