Hey there! I’m new to pagination in django and I wanted to create a model which
a user can add multiple values in a field, so I searched the Internet and
stumbled upon using “admin.StackedInline” and “inlines” to a model. It worked
in the admin dashboard there are now options to add multiple data to inlines.
But the problem is, when I tried looping the querysets and paginating it I got the results (see images below) on the browser.
As you guys can see, even though the geohazard fields has multiple values, it only shows one inline data in the page (1st pic was barangay_img) & (2nd pic was geohazard_img). What I really want is to loop the entire inlines (geohazard_imgs) and display it in one page not separately with it’s associated parent field ‘history’. How am I going to achieve this in my templates and views?
Sorry for my confusing explanation I’m honestly a student and a complete django beginner.
models.py
class History(models.Model):
BARANGAY = (
('Alegria','Alegria'),
('Bagacay','Bagacay'),
('Baluntay','Baluntay'),
('Datal Anggas','Datal Anggas'),
('Domolok','Domolok'),
('Kawas','Kawas'),
('Ladol','Ladol'),
('Maribulan','Maribulan'),
('Pag-Asa','Pag-Asa'),
('Paraiso','Paraiso'),
('Poblacion','Poblacion'),
('Spring','Spring'),
('Tokawal','Tokawal')
)
barangay_name = models.CharField(max_length=100,choices=BARANGAY,default='Alegria')
barangay_img = models.ImageField(upload_to='history_imgs',blank=True)
barangay_info = models.TextField()
def __str__(self):
return f"{self.barangay_name} - GeoHazard History"
class Meta:
verbose_name = 'History'
verbose_name_plural = 'Histories'
ordering = ['barangay_name']
class GeoHazard(models.Model):
history = models.ForeignKey(History, related_name='geohazards', on_delete=models.CASCADE)
geohazard_img = models.ImageField(upload_to='history_imgs',blank=True)
date_published = models.CharField(max_length=100, null=True)
geohazard_info = models.TextField()
def __str__(self):
return f"GeoHazard Details - {self.id}"
class Meta:
verbose_name = 'GeoHazard'
verbose_name_plural = 'GeoHazards'
class Assessment(models.Model):
RATINGS = (
('HIGH','HIGH'),
('HIGH (Mitigated)','HIGH (Mitigated)'),
('MODERATE','MODERATE'),
('MODERATE (Mitigated)','MODERATE (Mitigated)'),
('LOW','LOW'),
('UNKNOWN','UNKNOWN'),
)
history = models.ForeignKey(History, related_name='assessment', on_delete=models.CASCADE)
purok_name = models.CharField(max_length=50)
purok_coordinates = models.CharField(max_length=100,default='unknown')
flood_rating = models.CharField(max_length=100,choices=RATINGS,default='UNKNOWN')
landslide_rating = models.CharField(max_length=100,choices=RATINGS,default='UNKNOWN')
def __str__(self):
return f"GeoHazard Assessment - {self.id}"
class Meta:
verbose_name = 'Assessment'
verbose_name_plural = 'Assessments'
views.py
class history(ListView):
model = GeoHazard
template_name = 'auxiliary/history.html'
context_object_name = 'geohazards'
paginate_by = 1
template.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
{% for hazard in geohazards %}
<h1> {{ hazard.history.barangay_name }}</h1>
<img src="{{hazard.history.barangay_img.url}}">
<p>{{hazard.history.barangay_info}}</p>
<img src="{{hazard.geohazard_img.url}}">
<p>{{hazard.geohazard_info}}</p>
{% endfor %}
{% if is_paginated %}
{% if page_obj.has_previous %}
<button><a href="?page={{page_obj.previous_page_number}}">Previous</a></button>
{% endif %}
<button><a href="?page=1">1</a></button>
<button><a href="?page=2">2</a></button>
<button><a href="?page=3">3</a></button>
<button><a href="?page=4">4</a></button>
<button><a href="?page=5">5</a></button>
<button><a href="?page=6">6</a></button>
{% if page_obj.has_next %}
<button><a href="?page={{page_obj.next_page_number}}">Next</a></button>
{% endif %}
{% endif %}
</body>
</html>
admin.py
from auxiliary.models import (
History,
GeoHazard,
Assessment
)
class GeoHazardInline(admin.StackedInline):
model = GeoHazard
extra = 0
class AssessmentInline(admin.StackedInline):
model = Assessment
extra = 0
class HistoryAdmin(admin.ModelAdmin):
inlines = [GeoHazardInline,AssessmentInline]
admin.site.register(History,HistoryAdmin)
Been at it for ages and looking forward to anyone who can help. Really eager to find the solution to this for the Project’s deadline is right at our doorsteps. Thanks and advance!