Question

I have a Django application with a number of polls. There is no option to register on my website as a user. I want to allow a visitor to vote on a specific poll only once every seven days.

This is how I am changing the visitor permission in my ajax.py file

@dajaxice_register
def update_disable(request, song_pk):
    #update votes
    request.session['has_voted'] = True
    request.session.set_expiry(timezone.now() + timedelta(days=7))
    return dajax.json()

This is how I'm disabling the button in my template

{% if perms.hunt.has_voted %}
    <button type="button" class="btn btn-default" disabled="disabled">
      Vote as Favourite
    </button>
{% else %}
    <button type="button" class="btn btn-default" onclick="update(this);">
      Vote as Favourite
    </button>
{% endif %}

But even after voting my code goes to the else part in the template. What's wrong here?

Était-ce utile?

La solution

Well what's wrong is that you are saving the flag into session, but for some reason you're trying to read perms.hunt.has_voted (this checks, if user hunt has permission to vote, but not if he already voted or not..), which is a value coming from permission app. It has nothing to do with session.

You need to access the session value from your template:

{% if request.session.has_voted %}
...

This will check if currently logged in user already voted, so if has_voted is False or the key doesn't exist, then user will be able to vote.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top