0

I'm working on a livescore app and can't get matches within leagues in the table, nested for loop is empty.

I only get listed leagues and I would like to have inside the leagues all the matches that belong to those leagues

-Leaugue

  • match
  • match

-League

  • match
  • match

My DB enter image description here

Models:

class basic_scores(models.Model):
   time_status = models.CharField(max_length=5)
   time_minute = models.CharField(max_length=5)
   localteam_data_name = models.CharField(max_length=255)
   visitorteam_data_name = models.CharField(max_length=255)
   scores_localteam_score = models.CharField(max_length=15)
   scores_visitorteam_score = models.CharField(max_length=15)
   country_name = models.CharField(null=True, max_length=255)
   league_name = models.CharField(null=True, max_length=255

View:

def league(request):
#live_list = football_nows.objects.all()
live_list = basic_scores.objects.all()
return render(request, 'league.html',
              {'live_list': live_list})

league.html:

    <h1>Live</h1>
{% for league in live_list %}
        <center>
        <td>{{ league.league_name }}</td> 
    {% for live in league %}
         <td>{{ live.time_minute }}</td>  
            <td>{{  live.localteam_data_name  }}</td>
            <td>{{  live.scores_localteam_score  }} : {{  live.scores_visitorteam_score  }}</td>
            <td>{{  live.visitorteam_data_name  }}</td>
        
    {% endfor %}
</center>
       {#  This is a comment. #}
{% endfor %}

Browser result:

enter image description here

2
  • Please edit and add the model you have for match to the question. Commented May 15, 2021 at 8:04
  • 1
    Model is inside of post Commented May 15, 2021 at 8:15

1 Answer 1

1

@Ronnin you need to modify your view and create context correctly to achieve your result.

Lets take it step by step:

Problem in your code. league is not a list or iterator or any kind. It is a single record.

{% for league in live_list %}
    <center>
    <td>{{ league.league_name }}</td>
{% for live in league %} // this is wrong
     <td>{{ live.time_minute }}</td>  
        <td>{{  live.localteam_data_name  }}</td>
        <td>{{  live.scores_localteam_score  }} : {{  live.scores_visitorteam_score  }}</td>
        <td>{{  live.visitorteam_data_name  }}</td>
    
{% endfor %}

Make your view to have right context.

def league(request):
#live_list = football_nows.objects.all()
live_list_obj = basic_scores.objects.all()

live_list = {}
for live in live_list_obj:
  if not live.league_name in live_list:
    live_list[live.league_name] = [live]
  else:
    live_list[live.league_name].append(live)

return render(request, 'league.html',
              {'live_list': live_list})

Here is the html for the view

    {% for league_name, league_details in live_list.items %}
    <center>
        <td>{{ league_name }}</td>
     {% for live in league_details %}
     <td>{{ live.time_minute }}</td>  
        <td>{{  live.localteam_data_name  }}</td>
        <td>{{  live.scores_localteam_score  }} : {{  live.scores_visitorteam_score  }}</td>
        <td>{{  live.visitorteam_data_name  }}</td>

{% endfor %}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much @ajay-rathore, it works !! One more question, is it possible to have something like this - Country-> Leugaue - match - match
@Ronnin this might be a little tricky. In our view you can update live_list[live.league_name] -> live_list[(live.country_name, live.league_name,)] Then in your template you would update it like this {{ league_name|first }} -> {{ league_name|last }}
Ok everything works great! but not all league games are shown, only one game per league is shown and there are multiple matches per league in the base. Thank you again for your help @ajay-rathore
I do recommend you to learn normalization on your db table. This would also make your views much easier to make too. guru99.com/database-normalization.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.