How to display results of SQLalchemy query correctly in Flask Jinja templates












0















I am having trouble displaying the results of a SQLAlchemy query correctly in a Jinja template page.



I am basing my project on Formula 1 Drivers and the Teams that they drive for. I have a page which lists the Teams in F1 and it should list the Drivers who race for each individual team (Many-to-Many relationship between Driver and Team classes).



The output I receive is correct, however it appears to be grouped together in a list type output. I would like the drivers for each team to be listed independently as strings rather than a list grouped together.



Here is my code:



models.py



from datetime import datetime
from app import db


class Team_driver(db.Model):
# Many-to-many relationship table between Driver and Team
__tablename__ = "team_driver"
id = db.Column(db.Integer, primary_key=True)
team_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=False)
driver_id = db.Column(db.Integer, db.ForeignKey('driver.id'), nullable=False)


class Team(db.Model):
__tablename__ = "team"
id = db.Column(db.Integer, primary_key=True)
teamName = db.Column(db.String(64))
nationality = db.Column(db.String(64))
# relationship between Team table and Driver table, reference 'team' in forms
drivers = db.relationship('Driver', secondary="team_driver", backref='team')

def __repr__(self):
# specify variables to return to web page from backref
return '{}'.format(self.teamName)


class Driver(db.Model):
__tablename__ = "driver"
id = db.Column(db.Integer, primary_key=True)
firstName = db.Column(db.String(64))
lastName = db.Column(db.String(64))
raceNum = db.Column(db.Integer)
nationality = db.Column(db.String(64))
# relationship between Driver table and Team table, reference 'driver' in forms
teams = db.relationship('Team', secondary="team_driver", backref='driver')

def __repr__(self):
# specify variables to return to web page from backref
return '{} {}'.format(self.firstName,self.lastName)


routes.py (only for specific route)



@app.route('/teamDetails', methods=['GET', 'POST'])
def teamDetails():
details = Team.query.all()
return render_template('teamDetails.html', title='Team Details', details=details)


driverDetails.html



<!-- teamDetails.html -->

<!-- all other html files must now extend index.html and not base.html -->
{% extends "index.html" %}

{% block content %}
<h1>Team Details</h1>
{% for d in details %}
<div><p>Team Name: {{ d.teamName }}<br>Nationality: {{ d.nationality }}
<br>Drivers: {{ d.driver }}</p></div>
{% endfor %}
{% endblock %}


Here is the output I get from the webpage



screenshot of webpage output



What I would like is the page to display the relevant drivers, but separately, without the [ ] brackets.



Can anyone help with where I am going wrong?



Many thanks.










share|improve this question























  • I think the Team.driver (per backref) is a tomany relationship so it renders as an array with . You can remove the brackets() with a join, eg. ','.join(driver) . Also, I think you should rename "details" to "teams".

    – Tohmaxxx
    Nov 21 '18 at 14:46
















0















I am having trouble displaying the results of a SQLAlchemy query correctly in a Jinja template page.



I am basing my project on Formula 1 Drivers and the Teams that they drive for. I have a page which lists the Teams in F1 and it should list the Drivers who race for each individual team (Many-to-Many relationship between Driver and Team classes).



The output I receive is correct, however it appears to be grouped together in a list type output. I would like the drivers for each team to be listed independently as strings rather than a list grouped together.



Here is my code:



models.py



from datetime import datetime
from app import db


class Team_driver(db.Model):
# Many-to-many relationship table between Driver and Team
__tablename__ = "team_driver"
id = db.Column(db.Integer, primary_key=True)
team_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=False)
driver_id = db.Column(db.Integer, db.ForeignKey('driver.id'), nullable=False)


class Team(db.Model):
__tablename__ = "team"
id = db.Column(db.Integer, primary_key=True)
teamName = db.Column(db.String(64))
nationality = db.Column(db.String(64))
# relationship between Team table and Driver table, reference 'team' in forms
drivers = db.relationship('Driver', secondary="team_driver", backref='team')

def __repr__(self):
# specify variables to return to web page from backref
return '{}'.format(self.teamName)


class Driver(db.Model):
__tablename__ = "driver"
id = db.Column(db.Integer, primary_key=True)
firstName = db.Column(db.String(64))
lastName = db.Column(db.String(64))
raceNum = db.Column(db.Integer)
nationality = db.Column(db.String(64))
# relationship between Driver table and Team table, reference 'driver' in forms
teams = db.relationship('Team', secondary="team_driver", backref='driver')

def __repr__(self):
# specify variables to return to web page from backref
return '{} {}'.format(self.firstName,self.lastName)


routes.py (only for specific route)



@app.route('/teamDetails', methods=['GET', 'POST'])
def teamDetails():
details = Team.query.all()
return render_template('teamDetails.html', title='Team Details', details=details)


driverDetails.html



<!-- teamDetails.html -->

<!-- all other html files must now extend index.html and not base.html -->
{% extends "index.html" %}

{% block content %}
<h1>Team Details</h1>
{% for d in details %}
<div><p>Team Name: {{ d.teamName }}<br>Nationality: {{ d.nationality }}
<br>Drivers: {{ d.driver }}</p></div>
{% endfor %}
{% endblock %}


Here is the output I get from the webpage



screenshot of webpage output



What I would like is the page to display the relevant drivers, but separately, without the [ ] brackets.



Can anyone help with where I am going wrong?



Many thanks.










share|improve this question























  • I think the Team.driver (per backref) is a tomany relationship so it renders as an array with . You can remove the brackets() with a join, eg. ','.join(driver) . Also, I think you should rename "details" to "teams".

    – Tohmaxxx
    Nov 21 '18 at 14:46














0












0








0








I am having trouble displaying the results of a SQLAlchemy query correctly in a Jinja template page.



I am basing my project on Formula 1 Drivers and the Teams that they drive for. I have a page which lists the Teams in F1 and it should list the Drivers who race for each individual team (Many-to-Many relationship between Driver and Team classes).



The output I receive is correct, however it appears to be grouped together in a list type output. I would like the drivers for each team to be listed independently as strings rather than a list grouped together.



Here is my code:



models.py



from datetime import datetime
from app import db


class Team_driver(db.Model):
# Many-to-many relationship table between Driver and Team
__tablename__ = "team_driver"
id = db.Column(db.Integer, primary_key=True)
team_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=False)
driver_id = db.Column(db.Integer, db.ForeignKey('driver.id'), nullable=False)


class Team(db.Model):
__tablename__ = "team"
id = db.Column(db.Integer, primary_key=True)
teamName = db.Column(db.String(64))
nationality = db.Column(db.String(64))
# relationship between Team table and Driver table, reference 'team' in forms
drivers = db.relationship('Driver', secondary="team_driver", backref='team')

def __repr__(self):
# specify variables to return to web page from backref
return '{}'.format(self.teamName)


class Driver(db.Model):
__tablename__ = "driver"
id = db.Column(db.Integer, primary_key=True)
firstName = db.Column(db.String(64))
lastName = db.Column(db.String(64))
raceNum = db.Column(db.Integer)
nationality = db.Column(db.String(64))
# relationship between Driver table and Team table, reference 'driver' in forms
teams = db.relationship('Team', secondary="team_driver", backref='driver')

def __repr__(self):
# specify variables to return to web page from backref
return '{} {}'.format(self.firstName,self.lastName)


routes.py (only for specific route)



@app.route('/teamDetails', methods=['GET', 'POST'])
def teamDetails():
details = Team.query.all()
return render_template('teamDetails.html', title='Team Details', details=details)


driverDetails.html



<!-- teamDetails.html -->

<!-- all other html files must now extend index.html and not base.html -->
{% extends "index.html" %}

{% block content %}
<h1>Team Details</h1>
{% for d in details %}
<div><p>Team Name: {{ d.teamName }}<br>Nationality: {{ d.nationality }}
<br>Drivers: {{ d.driver }}</p></div>
{% endfor %}
{% endblock %}


Here is the output I get from the webpage



screenshot of webpage output



What I would like is the page to display the relevant drivers, but separately, without the [ ] brackets.



Can anyone help with where I am going wrong?



Many thanks.










share|improve this question














I am having trouble displaying the results of a SQLAlchemy query correctly in a Jinja template page.



I am basing my project on Formula 1 Drivers and the Teams that they drive for. I have a page which lists the Teams in F1 and it should list the Drivers who race for each individual team (Many-to-Many relationship between Driver and Team classes).



The output I receive is correct, however it appears to be grouped together in a list type output. I would like the drivers for each team to be listed independently as strings rather than a list grouped together.



Here is my code:



models.py



from datetime import datetime
from app import db


class Team_driver(db.Model):
# Many-to-many relationship table between Driver and Team
__tablename__ = "team_driver"
id = db.Column(db.Integer, primary_key=True)
team_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=False)
driver_id = db.Column(db.Integer, db.ForeignKey('driver.id'), nullable=False)


class Team(db.Model):
__tablename__ = "team"
id = db.Column(db.Integer, primary_key=True)
teamName = db.Column(db.String(64))
nationality = db.Column(db.String(64))
# relationship between Team table and Driver table, reference 'team' in forms
drivers = db.relationship('Driver', secondary="team_driver", backref='team')

def __repr__(self):
# specify variables to return to web page from backref
return '{}'.format(self.teamName)


class Driver(db.Model):
__tablename__ = "driver"
id = db.Column(db.Integer, primary_key=True)
firstName = db.Column(db.String(64))
lastName = db.Column(db.String(64))
raceNum = db.Column(db.Integer)
nationality = db.Column(db.String(64))
# relationship between Driver table and Team table, reference 'driver' in forms
teams = db.relationship('Team', secondary="team_driver", backref='driver')

def __repr__(self):
# specify variables to return to web page from backref
return '{} {}'.format(self.firstName,self.lastName)


routes.py (only for specific route)



@app.route('/teamDetails', methods=['GET', 'POST'])
def teamDetails():
details = Team.query.all()
return render_template('teamDetails.html', title='Team Details', details=details)


driverDetails.html



<!-- teamDetails.html -->

<!-- all other html files must now extend index.html and not base.html -->
{% extends "index.html" %}

{% block content %}
<h1>Team Details</h1>
{% for d in details %}
<div><p>Team Name: {{ d.teamName }}<br>Nationality: {{ d.nationality }}
<br>Drivers: {{ d.driver }}</p></div>
{% endfor %}
{% endblock %}


Here is the output I get from the webpage



screenshot of webpage output



What I would like is the page to display the relevant drivers, but separately, without the [ ] brackets.



Can anyone help with where I am going wrong?



Many thanks.







python flask jinja2 flask-sqlalchemy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 14:11









kdouglakdougla

32




32













  • I think the Team.driver (per backref) is a tomany relationship so it renders as an array with . You can remove the brackets() with a join, eg. ','.join(driver) . Also, I think you should rename "details" to "teams".

    – Tohmaxxx
    Nov 21 '18 at 14:46



















  • I think the Team.driver (per backref) is a tomany relationship so it renders as an array with . You can remove the brackets() with a join, eg. ','.join(driver) . Also, I think you should rename "details" to "teams".

    – Tohmaxxx
    Nov 21 '18 at 14:46

















I think the Team.driver (per backref) is a tomany relationship so it renders as an array with . You can remove the brackets() with a join, eg. ','.join(driver) . Also, I think you should rename "details" to "teams".

– Tohmaxxx
Nov 21 '18 at 14:46





I think the Team.driver (per backref) is a tomany relationship so it renders as an array with . You can remove the brackets() with a join, eg. ','.join(driver) . Also, I think you should rename "details" to "teams".

– Tohmaxxx
Nov 21 '18 at 14:46












1 Answer
1






active

oldest

votes


















0














Since d.driver is a list of drivers, you should print each item of the list separately, because if you print a list of something, it will also include the brackets.



{% block content %}
<h1>Team Details</h1>
{% for d in details %}
<div><p>Team Name: {{ d.teamName }}<br>Nationality: {{ d.nationality }}
<br>Drivers:
{% for driver in d.drivers %}
{{ driver }}
{% endfor %}
</p></div>
{% endfor %}
{% endblock %}





share|improve this answer
























  • Perfect, that did the job brilliantly. Thanks very much!

    – kdougla
    Nov 22 '18 at 8:23











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413957%2fhow-to-display-results-of-sqlalchemy-query-correctly-in-flask-jinja-templates%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Since d.driver is a list of drivers, you should print each item of the list separately, because if you print a list of something, it will also include the brackets.



{% block content %}
<h1>Team Details</h1>
{% for d in details %}
<div><p>Team Name: {{ d.teamName }}<br>Nationality: {{ d.nationality }}
<br>Drivers:
{% for driver in d.drivers %}
{{ driver }}
{% endfor %}
</p></div>
{% endfor %}
{% endblock %}





share|improve this answer
























  • Perfect, that did the job brilliantly. Thanks very much!

    – kdougla
    Nov 22 '18 at 8:23
















0














Since d.driver is a list of drivers, you should print each item of the list separately, because if you print a list of something, it will also include the brackets.



{% block content %}
<h1>Team Details</h1>
{% for d in details %}
<div><p>Team Name: {{ d.teamName }}<br>Nationality: {{ d.nationality }}
<br>Drivers:
{% for driver in d.drivers %}
{{ driver }}
{% endfor %}
</p></div>
{% endfor %}
{% endblock %}





share|improve this answer
























  • Perfect, that did the job brilliantly. Thanks very much!

    – kdougla
    Nov 22 '18 at 8:23














0












0








0







Since d.driver is a list of drivers, you should print each item of the list separately, because if you print a list of something, it will also include the brackets.



{% block content %}
<h1>Team Details</h1>
{% for d in details %}
<div><p>Team Name: {{ d.teamName }}<br>Nationality: {{ d.nationality }}
<br>Drivers:
{% for driver in d.drivers %}
{{ driver }}
{% endfor %}
</p></div>
{% endfor %}
{% endblock %}





share|improve this answer













Since d.driver is a list of drivers, you should print each item of the list separately, because if you print a list of something, it will also include the brackets.



{% block content %}
<h1>Team Details</h1>
{% for d in details %}
<div><p>Team Name: {{ d.teamName }}<br>Nationality: {{ d.nationality }}
<br>Drivers:
{% for driver in d.drivers %}
{{ driver }}
{% endfor %}
</p></div>
{% endfor %}
{% endblock %}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 15:05









JoostJoost

2,0421217




2,0421217













  • Perfect, that did the job brilliantly. Thanks very much!

    – kdougla
    Nov 22 '18 at 8:23



















  • Perfect, that did the job brilliantly. Thanks very much!

    – kdougla
    Nov 22 '18 at 8:23

















Perfect, that did the job brilliantly. Thanks very much!

– kdougla
Nov 22 '18 at 8:23





Perfect, that did the job brilliantly. Thanks very much!

– kdougla
Nov 22 '18 at 8:23


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413957%2fhow-to-display-results-of-sqlalchemy-query-correctly-in-flask-jinja-templates%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy