Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying property object instead of value in Flask template [closed]

In my Python (3.5) Flask project, I am using the CAS client for authentication and everything works fine. Once the authentication is done, I want to display the username in my output page. So I use the following code

__init__.py:

from flask import Flask
from flask_cas import CAS
from flask_cas import login_required
# ...

APP = Flask(__name__)
cas = CAS(APP)
# ...

def list_page()
     return render_template("list.html", name=cas.username)

@app.route('/admin/')
@login_required
def admin_page()
    return list_page();

list.html:

...
<H2> Hi {{name}} </h2>
...

At the output page I am seeing

Hi <property object at 0x7fb37dfe6958>
like image 937
Md Monjur Ul Hasan Avatar asked Jan 29 '26 09:01

Md Monjur Ul Hasan


1 Answers

It's hard to pinpoint exactly what the problem is without knowing what your cas variable is, in __init__.py. However I suspect the error you have might be because cas is a class, rather than an instance of that class.

To show you what I mean:

>>> class MyCAS:
...    @property
...    def name(self):
...        return 'Monjur'
...
>>> MyCAS.name
<property object at 0x109cf1cc8>
>>> MyCAS().name
'Monjur'

In the first case, I have not instantiated the class (no parenthesis), so the return value is the property object. In the second case (with parenthesis), I see the actual name of the instance.

If your cas object is indeed supposed to be an object instantiated from a class (e.g. flask.ext.cas.CAS), make sure you create it like this:

from flask.ext.cas import CAS
cas = CAS() # With parenthesis :)

and not like this:

from flask.ext.cas import CAS
cas = CAS  # No parenthesis :(
like image 191
Samuel Dion-Girardeau Avatar answered Jan 31 '26 02:01

Samuel Dion-Girardeau