Created
September 29, 2013 05:56
-
-
Save mitchdowney/6749763 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
candidates.py | |
class CandidateDetailView(DetailView): | |
model = Candidate | |
context_object_name = 'candidate' | |
def get_context_data(self, **kwargs): | |
context = super(CandidateDetailView, self).get_context_data(**kwargs) | |
context['election'] = self.get_object().election | |
context['previous_candidates'] = Candidate.objects.order_by('id').filter(election_id=self.get_object().election.id).filter(is_approved=True).filter(id__lt=self.get_object().id) | |
context['next_candidates'] = Candidate.objects.order_by('id').filter(election_id=self.get_object().election.id).filter(is_approved=True).filter(id__gt=self.get_object().id) | |
return context | |
template | |
{% for candidate in next_candidates|slice:":1" %} | |
<a class="infobox-next-link" href="/candidates/{{ candidate.id}}">Next Candidate</a> | |
{% endfor %} | |
{% for candidate in previous_candidates|dictsortreversed:"id"|slice:":1" %} | |
<a class="infobox-previous-link" href="/candidates/{{ candidate.id}}">Previous Candidate</a> | |
{% endfor %} | |
boomshakalaka |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I see-- so those giant filter statements query all the candidates and remove the current candidate and all prior. Seems like it could work-- though I question it's performance. What happens when you hit the last one?