everyvote / everyvote_mini

Election information app for university student governments and other organizations
www.everyvote.org
1 stars 0 forks source link

How do I add a Edit Profile button to the header IF the logged-in user is a candidate in the currently open election? #21

Closed mitchdowney closed 11 years ago

mitchdowney commented 11 years ago

I'm real perplexed by this issue. I was able to get something similar to work moderators, as now if the logged-in user is a moderator for an election they are viewing, the "Edit Election" button will appear in the header. The code I used to make that work is:

{% for moderator in election.moderators.all %}
    {% if user == moderator %}
        <li><a href="{% url 'election_update' pk=object.pk %}" class="btn btn-small btn-primary"><i class=icon-pencil icon-white"></i>Edit Election</a></li>
    {% endif %}
{% endfor %}

So I tried to make an "Edit Profile" button the same way, that would only appear if the logged-in user is a candidate in the election they are viewing. I can't fer the life of me figure out why this isn't working:

{% for candidate in election.candidate_set.all %}
    {% if user == candidate.user %}
        <li><a href="{% url 'election_update' pk=object.pk %}" class="btn btn-small btn-primary"><i class=icon-pencil icon-white"></i>Edit Profile</a></li>
    {% endif %}
{% endfor %}

What's weirdest for me is that I can verify that the for loop for election.candidate_set.all is returning the username of all the candidates in the currently open election, and user is returning the username of the currently logged-in user. But regardless {% if user == candidate.user %} never seems to return a result of True.

Any ideas what I'm doing wrong?

The relevant pages are election_detail.html, logos.views, everyvote_mini.urls, and everyvote_mini.models.

mitchdowney commented 11 years ago

Vince figured it out. We need to use {% if user.pk == candidate.user.pk %} instead. We're not sure why the pk is necessary, because Django's documentation seems to say {% user == candidate.user %} should work. We'll have to look into this a little more sometime...