How can I make a class variable available to Jinja2 templates with Flask?

This page summarizes the projects mentioned and recommended in the original post on /r/codehunter

InfluxDB - Power Real-Time Data Analytics at Scale
Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
www.influxdata.com
featured
SaaSHub - Software Alternatives and Reviews
SaaSHub helps you find the best software and product alternatives
www.saashub.com
featured
  • flasky

    Companion code to my O'Reilly book "Flask Web Development", second edition.

  • Flask I have a template that is using flask-login and I have a permission model built on bitsets. In my template, I want to expose content in a page based on permissions. I have a function can(permission) in my user model that controls what a user can do. class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True) fullname = db.Column(db.String(75)) password_hash = db.Column(db.String(128)) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify\_password(self, password): return check_password_hash(self.password_hash, password) def can(self, permissions): return self.role is not None and (self.role.permissions & permissions) == permissions def __repr__(self): return ''.format(self.email)class Permission: FOLLOW = 0x01 COMMENT = 0x02 WRITE_ARTICLES = 0x04 MODERATE_COMMENTS = 0x08 ADMINISTER = 0x80 When I try to call the can() function in my template and pass it a permission, I get an error that Permission is not defined, presumably because the Permission class is not in scope for the template. Here is part of my base.html that all other templates extend: {% if current_user.can(Permission.ADMINISTER) %}Administration Add User Change Password Lock User{% endif %} I'm loosely following Miguel Grinberg's book on Flask Web Development and he is doing the same thing in his base.html (line 32). How can I make the Permission class available to my template? Answer link : https://codehunter.cc/a/flask/how-can-i-make-a-class-variable-available-to-jinja2-templates-with-flask

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts

  • Show HN: Offline Renderer for GitHub flavoured Markdown

    2 projects | news.ycombinator.com | 2 Apr 2024
  • This Week In Python

    5 projects | dev.to | 2 Mar 2024
  • Simple Flask Integration for an Elastic Semantic Search App

    2 projects | dev.to | 6 Feb 2024
  • Ask HN: Washed out PHP Dev – What to do next?

    2 projects | news.ycombinator.com | 27 Dec 2023
  • The Flask Mega-Tutorial, Part I: Hello, World

    1 project | news.ycombinator.com | 12 Dec 2023