Greetings!
I can display a list of pages from entry forms that create objects submitted in my app it looks as follows:
Casa object (1)
Casa object (2)
Casa object (3)
Casa object (4)
Casa object (5)
Casa object (6)
Casa object (7)
Casa object (8)
Casa object (9)
Casa object (10)
Casa object (11)
Casa object (12)
Casa object (13)
Casa object (14)
My source is the pagination documentation, I am attempting to change my views to display 1 object at a time.
Method, take this:
def home(request):
context = {
'casa' : Casa.objects.all()
}
return render(request,'CMS/home.html',context)
and do this:
def home(request):
student_list = Casa.objects.all()
paginator = Paginator (student_list,1)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
context = {
'casa' : Casa.objects.all()
}
return render(request,'CMS/home.html',{'page_obj':page_obj})
any help is appreciated kind Django brethren!
best,
Bryan
Take a wild guess here - what do you think that that error message is telling you?
Or, what results do you get if you put that specific phrase into the search bar on the forum? (The “magnifying glass” icon on the top right.)
Ken!!
You are always so helpful! I utilized the aforementioned sherlock device found, “adding an order_by clause on the queryset or an ordering in the Meta for that model.” (@KenWhitesell )
I’d like to sort the object list in the meta data. Where does the sort function fit properly in my views.py?
Entry.objects.order_by('model')
my class in views.py:
class StudentForm(ModelForm):
class Meta:
model = models.Casa
fields = ['Student','Cohort','Phone_Number','Email_Address','Home_Address','City','State','Zip_Code']
@property
def helper(self):
helper =FormHelper()
helper.layout = Layout(
HTML('<h2> Submit Entry</h2>'),
# Field('Student'),
# Field('Cohort'),
# Field('Phone_Number'),
# Field('Email_Address'),
# Field('Home_Address'),
# Field('City'),
# Field('State'),
# Field('Zip_Code'),
# Submit('submit','Submit Entry',css_class='btn-success')
)
for field in self.Meta().fields:#loop through fields instead..
helper.layout.append(
Field(field, wrapper_class='row')
)
helper.layout.append(Submit('submit','Submit Entry',css_class='btn-success'))
helper.field_class = 'col-9'
helper.label_class = 'col-3'
return helper
That’s your form, not your view.
You can apply the sort to the queryset retrieving the objects being displayed.
Or, as you wrote:
You set that ordering in the Meta class for the model, not the form or the view.
See Models | Django documentation | Django and Model Meta options | Django documentation | Django.
1 Like
Ken,
Thank you and noted. Set the ordering in the meta class for that model. This cleared the error, after a migration, but the data is not paginated/displayed when the url is reached. Thank you very much for your help. I will research the links above to see if I can see why (:
Have a great week!
B