I am trying to work with the {% regroup %}
template tag to display user made lists with items in them to look like this:
LIST NAME
Item
Item
Item
Not this (which is the only way I can get it to render atm):
List Name 1
Item 1
List Name 1
Item 2
List Name 1
Item 3
Based on my reading of the Django Docs I have to turn my queryset into a dictionary but I am having trouble achieving this. I also need to ensure my queryset if filtered based on the logged in user. I am getting errors depending on where I put the values() method, or it currently shows nothing at all...
I also tried following this answer but got no love.enter link description here
Here is my views.py
class user_playlist(ListView):
template_name = 'testingland/playlist.html'
context_object_name = 'playlist'
model = UserVenue
def get_queryset(self):
venue = self.request.GET.get('venue', None)
list = self.request.GET.get('list', None)
playlist = UserVenue.objects.filter(list__user=self.request.user)
return playlist
Here is the models:
class UserList(models.Model):
list_name = models.CharField(max_length=255)
user = models.ForeignKey(User, on_delete=models.CASCADE) #is this okay?
def __str__(self):
return self.list_name
class UserVenue(models.Model):
venue = models.ForeignKey(mapCafes, on_delete=models.PROTECT)
list = models.ForeignKey(UserList, on_delete=models.PROTECT)
And here is the template (I built it based on reading the Django Docs on regroup).
<div class="user_playlists">
{% regroup venue by list as user_playlist %}
<ul>
{% for list in user_playlist %}
<li> {{ list.grouper }}
<ul>
{% for place in list.list %}
<li>{{ place.venue }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</div>