Tshibe
Tshibe

Reputation: 59

Remove brackets from selected row sqlalchemy

How can I remove all brackets and marks using python. Here's my code in flask-python:

@app.route('/login',methods=['GET','POST'])
def login():  
  username = db.session.query(User.username).order_by(User.username.desc()).limit(1).all()
        return render_template("index.html",user = username)

and in my html file just

{{user}}

and the result in browser is like:

[('tshibek',)]

How can I remove all of this and to only get the nickname tshibek.

The second question is I want to have an example of 10 last rows (I know how do that) but what I don't know is how to display this nickname like links to the profile, every nickname to another line like the link to the profile.

example:
I have rows nickname:
tshibe,tshibek,wasda,something
and i wanna get in HTML link
localhost//profil/tshibe
localhost//profil/tshibek
localhost//profil/wasda

... I dont know how to start with that, can someone show me a way to do that ?

Upvotes: 0

Views: 964

Answers (1)

Haleemur Ali
Haleemur Ali

Reputation: 28283

use scalar instead of all

scalar will extract the first value in the tuple returned

so, [('tshibek',)] will be returned as 'tshibek'.

@app.route('/login',methods=['GET','POST'])
def login():  
  username = db.session.query(User.username).order_by(User.username.desc()).limit(1).scalar()
        return render_template("index.html",user = username)

if you have a nicknames in a list of tuples

rows = [('tshibe',),('tshibek',),('wasda',),('something')]

extract the nicknames using list comprehension

nicks = [x[0] for x in rows]

This will return a list like this:

['tshibe','tshibek','wasda','something']

you can prepend the base url to it like this

base = 'http://localhost/profil'
urls = ['{}/{}'.format(base, n) for n in nicks]

Upvotes: 2

Related Questions