How to check the total views on a page-flask?

I’m building an app which allows users to look in to view my cv. I want to see how many views each page has and from which user.

I have a visit table which I’m hoping will store the pagename along with the user Id every time a page is viewed. So every record will be a new visit to that page.

Any suggestions or a better way to do this?

model.py

class  User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    visits = db.relationship('Visit', backref='user', lazy='dynamic')
    password_hash = db.Column(db.String(128))

    def __repr__(self):
        return 'Username: {}'.format(self.username)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def update_cv_looks(self):
        self.cv_looks += 1
        db.session.add(self)
        db.session.commit()


    def page_visits(self, pagename):
        list_of_visits = self.visits.filter_by(page_name=pagename).all()
        return len(list_of_visits)


class Visit(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    page_name = db.Column(db.String(25))

    def __repr__(self):
        return 'Page: {}'.format(self.page_name)

routes.py

@app.route('/')
@app.route('/cover')
@login_required
def cover():
    return render_template('cover.html', title='Cover Page')


@app.route('/index')
@login_required
def index():
    current_user.page_visits('index')
    return render_template(
        'index.html', title='Home Page')

Answer

Place this in the model.py under the Visit class.

def update_visits(self, user_id, pagename):
self.user_id, self.page_name = user_id, pagename
db.session.add(self)
db.session.commit()

Place this under the index routes.py.

v = Visit()
v.update_visits(user_id=current_user.id, pagename='index')