Reputation: 101
My models come from a postgres database. I have gotten data from both of my models to display in the browser (CurrentAlLeagueStandings and CurrentNlLeagueStandings). However, I can only get data from one model to display at a time. How do I get it to display data from each model, at the same time, on the same webpage. The code does work when I try to pull from only one model. Something to also note is that both of these models have the same field names.
If anybody can help it would be greatly appreciated. An example relative to my code would be a huge help!
https://gist.github.com/anonymous/052b91ea56a951ac3fbc5ab7f66cc9f8
views.py
from django.shortcuts import render
from standings.models import CurrentAlLeagueStandings
from standings.models import CurrentNlLeagueStandings
def currentleaguestandings(request):
data = CurrentAlLeagueStandings.objects.all()
return render(request, 'standings/league.html', {"data": data})
data2= CurrentNlLeagueStandings.object.all()
return render(request, 'standings/league.html', {"data2": data2})
And the template rendered:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
#table to display data from CurrentAlLeagueStandings
<table>
<tr>
<th>Team Name</th>
</tr>
{% for standings in data %}
<tr>
<td>{{ standings.team }}</td>
</tr>
{% endfor %}
</table>
#table to display data from CurrentNlLeagueStandings
<table>
<tr>
<th>Team Name</th>
</tr>
{% for standings in data2 %}
<tr>
<td>{{ standings.team }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
Upvotes: 1
Views: 36
Reputation: 12005
What you can do is, you can pass a dictionary with as many data variables as you like, you were rendering your template twice, that's why you could only see data from the second rendering:
def currentleaguestandings(request):
data = CurrentAlLeagueStandings.objects.all()
data2= CurrentNlLeagueStandings.objects.all()
return render(request, 'standings/league.html', {"data": data, "data2": data2})
Upvotes: 1