Skip to content

Instantly share code, notes, and snippets.

@mitchdowney
Created September 29, 2013 05:56
Show Gist options
  • Save mitchdowney/6749763 to your computer and use it in GitHub Desktop.
Save mitchdowney/6749763 to your computer and use it in GitHub Desktop.
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
@scvnc
Copy link

scvnc commented Sep 29, 2013

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment