0

I've been following a course, and I want to change my data source from sqlite to mssql.

I've made the connection, and i'm trying to list the users in my db. But when I do I get the result show below:
<Users 2>
<Users 3>
Instead of showing the actual user data.

Ive uploaded the code to git: https://github.com/Desc83/flask_mssql

And it shows like this instead of showing the actual data?

import urllib


import os
import pyodbc 
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
from flask import Flask, render_template, url_for, redirect
from flask import Flask
from flask import current_app




app = Flask(__name__)


app.config["SQLALCHEMY_DATABASE_URI"] = "mssql://@Localhost/FlaskMSSQL?driver=ODBC Driver 17 for SQL Server"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False



db = SQLAlchemy(app)

Migrate(app,db)
db.init_app(app)


class Users(db.Model):

    __tablename__ = 'users'

    id = db.Column(db.Integer,primary_key= True)
    Username = db.Column(db.Text)
    Password = db.Column(db.Text)
    Email = db.Column(db.Text)

def __init__(self,name):
        self.name = name
  


@app.route('/users')
def listUsers():
    users = Users.query.all()
    test = Users.
    return render_template('listuers.html', users=users)
    
  
  
  
  
@app.route('/')
def index():
    return render_template('home.html')




if __name__ == '__main__':
        app.run(debug=True)

view:

{% extends "base.html" %}
{% block content %}
<div class="jumbotron">
  
  <p>list users</p>
  <ul>
    
    {% for user in users  %}
    <li>{{user}}</li>
    {% endfor %}
    
    {% for item in user %}
    <li>{{item}}</li>
    
    {% endfor %}
  </ul>
</div>
{% endblock %}

I have tried to split the data into bits, but it comes with the same result:

{% extends "base.html" %}
{% block content %}
<div class="jumbotron">
  
  <p>list users</p>
  <ul>
    
    {% for user in users  %}
    <li>{{user}}</li>
    {% endfor %}
    
    {% for item in user %}
    <li>{{item}}</li>
    
    {% endfor %}
  </ul>
</div>
{% endblock %}

1 Answer 1

1

It appears as if you need to specify the __str__ for the Users class.

Alternatively, you can explicitly indicate the fields you want to display in the template.

Instead of this:

{% for user in users  %}
<li>{{user}}</li>
{% endfor %}

Try this:

{% for user in users  %}
<li>{{user.Username}}</li>
{% endfor %}
Sign up to request clarification or add additional context in comments.

2 Comments

Just to clarify, you're showing the entire user object but what you want is just a part of that object. (nice documentation supporting your question, BTW)
Hi Mark, thank you. It seems to work. I tried adding def __repr__(self): return f"User: ('{self.username}', '{self.email}')" def __str__(self): return f"User: ('number {id} is: {self.username}', '{self.email}')" But with same result?

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.