HOW TO OUTPUT DATA GOT FROM combined queryset IN DJANGO TEMPLATE (html file) ?

49 views
Skip to first unread message

Byansi Samuel

unread,
Jan 12, 2023, 10:27:03 AM1/12/23
to ry...@fattuba.com, harshsp...@gmail.com, alex....@gmail.com, jjohn...@gmail.com, noel...@xtra.co.nz, django...@googlegroups.com, dav...@uniquode.io, di...@yodaplus.com, ja...@journeetrips.com, mi...@dewhirst.com.au, mare...@gmail.com, dilmurodd...@gmail.com, harrisl...@gmail.com
Hey!  Am having a problem. And l request for your Help.

Am having 3 apps in Django project
- action
- adventure
- others


#action/ models.py

....
class Action(models.Model):
    name=models.Charfield()
    os= models.Charfield( choices=OS)....



#adventure/models.py


....
class Adventure(models.Model):
     name=models.Charfield()
     os= models.Charfield( choices=OS)....



#Others/views.py


from itertools import chain
from action.models import Action
from adventure.models import Adventure

def windows_games(request):
    win_action = Action.objects.filter(os='windows')
    win_adventure = Adventure.objects.filter(os='windows')
    combined_list = list(chain(win_action,win_adventure))
    context = ['combined_list':combined_list,] 
         return render(request, 'others/os/windows_game.html' , context)



#others/os/windows_game.html

.....
<div class="container">
 <img src="{{combined_list.game_pic}}">
 <p>{{combined_list.name}}</p>
.....








1). I need to correct me in #others/ views.py  if there is any mistake done.

2). I would like to know how to know how to write the tag that outputs the results in #others/os/windows_game.html because I tried that but outputs nothing.
And l would like to be in form of,              #{{combined_list.game_pic}}, etc


Please help me.
I'm Samuel



Ryan Nowakowski

unread,
Jan 13, 2023, 5:59:57 PM1/13/23
to django...@googlegroups.com
On Thu, Jan 12, 2023 at 11:51:35AM +0300, Byansi Samuel wrote:
> #Others/views.py
>
>
> from itertools import chain
>
> from action.models import Action
>
> from adventure.models import Adventure
>
>
> def windows_games(request):
>
> win_action = Action.objects.filter(os='windows')
>
> win_adventure = Adventure.objects.filter(os='windows')
> combined_list = list(chain(win_action,win_adventure))
>
> context = ['combined_list':combined_list,]

This gives me a SyntaxError so I'm surprised you're not seeing the
same error. Your context looks like a list(square brackets) but you
probably want a dictionary(curly brackets).

>
> return render(request, 'others/os/windows_game.html' , context)
>
>
>
> #others/os/windows_game.html
>
> .....
> <div class="container">
> <img src="{{combined_list.game_pic}}">
> <p>{{combined_list.name}}</p>

combined_list is a list of games, not a single game, so you need to
iterate over it. Ex:

{% for game in combined_games %}
<img src="{{game.game_pic}}">
<p>{{game.name}}</p>
{% endfor %}

Hector Mwaky

unread,
Jan 14, 2023, 12:35:54 PM1/14/23
to Django users
  1. In the windows_games view in others/views.py, you are using the filter() method on the Action and Adventure models to get the action and adventure games that have the 'os' field set to 'windows'. Then you are using the itertools.chain() function to combine the two querysets into a single list, and storing that list in the variable combined_list. The context variable is not created correctly, it should be context = {'combined_list': combined_list}

  2. In the template others/os/windows_game.html, to output the results of the combined_list you should use a for loop to iterate over the list and display each item. Also, it seems that you are trying to access the game_pic attribute of the objects in the combined_list, but the models you have defined (Action and Adventure) do not have a field called game_pic

  3. {% for game in combined_list %} <img src="{{ game.game_pic }}"> <p>{{ game.name }}</p> {% endfor %}

  4. It would be a good idea to add a __str__ method to the models in order to return the name of the game, and also to add a field game_pic if you want to display the image of the game.

    class Action(models.Model): name=models.Charfield() os= models.Charfield(choices=OS) game_pic = models.ImageField(upload_to='action_game_pic/') def __str__(self): return

Byansi Samuel

unread,
Jan 16, 2023, 11:49:22 PM1/16/23
to django...@googlegroups.com
Thanks it worked but I would like to ask you, why did you use  " game" in  {%for game in combined_list %} ? En what is the role of 'game'


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0b3af5d4-bb40-430c-9016-c2c7fa6108fan%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages