Home > Blockchain >  Django Loading up Class based model data on a template
Django Loading up Class based model data on a template

Time:06-16

I am new to django before this only made 1 project and that was just a follow along video so obviously the fix is probably very easy, but I am trying to make a restaurant reservation page, and later on there will be user authentication to only show the reservation that particular user made, but right now I want to display all of the reservations in my modal and trying to learn how to actually display the data.

here's the modal: https://gyazo.com/066e3e060492990008d608a012f588f3

here's the view: https://gyazo.com/6947ed97d84b38f1e73680e28f3a0a9a

Here's the template: https://gyazo.com/966c4810b3c7f4dd8dad2e5b71c2179c

I am spent about 3 hours watching other videos on people loading there modal data in there website and when they do it I understand everything but for some reason I can't apply that to my own project, my guess is my for loop is wrong since I have a hard time with writing them and seem to always mess up, so any help so I can at least start looking in the right direction would be appreciated

CodePudding user response:

When using class based views you'll have to work with django conventions. For example where you have reservations = Reservation.objects.all(), reservations is not a defined class attribute for the class based view. What you can do is rename it to queryset instead.

from django.views.generic import ListView

class ReservationList(ListView):
     model = Reservation
     queryset = Reservation.objects.all()
     context_object_name = 'reservations'  # using this attribute to alias the queryset
     template_name = "make_a_reservation.html"

This way you can now use the name reservations in your template as you did with:

{% for i in reservations %}
     ...
{% endfor %}

That should work.

  • Related