Friday, January 13, 2023

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

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 %}

> 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

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20230113225914.GB28105%40fattuba.com.

No comments:

Post a Comment