1๐
OK I managed to get it to work by changing my Views.py:
def entry(request, projects_id=1, entries_id=1):
current_entry = Entries.objects.get(project_id=projects_id, entry_unique=entries_id)
current_entry_unique = current_entry.entry_unique
entry_previous = current_entry_unique
entry_previous -= 1
if Entries.objects.filter(project_id=projects_id, entry_unique=entry_previous).exists():
prev_entry = Entries.objects.get(project_id=projects_id, entry_unique=entry_previous)
else:
prev_entry = None
entry_next = current_entry.entry_unique
entry_next += 1
if Entries.objects.filter(project_id=projects_id, entry_unique=entry_next).exists():
next_entry = Entries.objects.get(project_id=projects_id, entry_unique=entry_next)
else:
next_entry = None
return render_to_response('entry.html',
{'projects': Projects.objects.get(id=projects_id),
'entry': Entries.objects.get(project_id=projects_id, entry_unique=entries_id),
'prev_entry': prev_entry,
'next_entry': next_entry }
)
And then my entry.html looks like this:
{% if prev_entry %}
<a href="/projects/get/{{projects.id}}/entries/get/{{entry.entry_unique|add:"-1"}}"><img src="/static/assets/img/arrow_left.png" width="40"></a>
{% endif %}
{% if next_entry %}
<a href="/projects/get/{{projects.id}}/entries/get/{{entry.entry_unique|add:"1"}}"><img src="/static/assets/img/arrow_right.png" width="40"></a>
{% endif %}
It may not be pretty but it is doing what I want it to do. There may be ways to get it to work more nicely, though.
๐คJason B
0๐
Without Paginator:
def entry(request, projects_id, entries_id):
entries_in_project = Entries.objects.filter(project_id=projects_id)
entry = get_object_or_404(entries_in_project, entry_unique=entries_id)
try:
prev_entry_id = entries_in_project.filter(entry_unique__lt=entries_id)
.order_by('-entry_unique').values('entry_unique')[:1][0]['entry_unique']
except IndexError:
prev_entry_id = None
try:
next_entry_id = entries_in_project.filter(entry_unique__gt=entries_id)
.order_by('entry_unique').values('entry_unique')[:1][0]['entry_unique']
except IndexError:
next_entry_id = None
return render_to_response('entry.html',{
'projects': Projects.objects.get(id=projects_id), 'entry': entry,
'next_entry_id': next_entry_id, 'prev_entry_id': prev_entry_id,
})
A Better Way:
All of this could be replaced with built-in functionality:
https://docs.djangoproject.com/en/dev/ref/models/options/#order-with-respect-to
class Entries(models.Model):
project_id = models.ForeignKey(Projects)
entry_pubdate = models.DateTimeField('date published')
class Meta:
order_with_respect_to = 'project_id'
def entry(request, projects_id, entries_id):
entry = get_object_or_404(Entries,project_id=projects_id, id=entries_id)
next_entry = entry.get_next_in_order()
prev_entry = entry.get_previous_in_order()
return render_to_response('entry.html',{
'projects': Projects.objects.get(id=projects_id), 'entry': entry,
'next_entry': next_entry, 'prev_entry': prev_entry,
})
In entry.html:
{% if next_entry %}<a href="{{ next_entry.get_absolute_url }}">Next</a>{% endif %}
{% if prev_entry %}<a href="{{ prev_entry.get_absolute_url }}">Previous</a>{% endif %}
๐คThomas
- [Answer]-Celery cannot import Schedules
- [Answer]-Couldn't load admin interface due the error below
- [Answer]-Django โ Creating Objects for models and saving it to MYSQL
Source:stackexchange.com