helloflask / bootstrap-flask

Bootstrap 4 & 5 helper for your Flask projects.

Home Page:https://bootstrap-flask.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Allow placeholders for other db model fields in `render_table` macro's action `url_for`s

caffeinatedMike opened this issue · comments

Can we implement a way to allow for placeholders of all fields in the db model?

{{ render_table(
       portal_list,
       titles=headers,
       responsive=True,
       show_actions=True,
       edit_url=url_for('portals.edit_portal', portal_slug=':slug'),
       delete_url=url_for('portals.delete_portal', portal_slug=':slug')
 ) }}

In this case slug is a field in the Portal model, but it is not the primary key, nor is its data show in one of the table's columns. The whole purpose of using the slug is for a user-friendly url and portals in this case are the top object that all things relate back to.

  • /<portal_slug>/edit
  • /<portal_slug>/delete
  • /<portal_slug>/accounts
  • /<portal_slug>/accounts/new
  • /<portal_slug>/accounts/1/edit
  • /<portal_slug>/accounts/1/delete
  • /<portal_slug>/reports
  • And so on
class Portal(db.Model):
    __tablename__ = "portals"

    id = db.Column(db.Integer, primary_key=True)
    slug = db.Column(db.String(20), nullable=False, unique=True)
    display_name = db.Column(db.String(100), nullable=False, unique=True)


portals = Blueprint("portals", __name__)

@portals.route("/<portal_slug>/edit", methods=["GET", "POST"])
def edit_portal(portal_slug):
    # Do stuff here
    return render_template("portal.html", form=form)

As a temporary fix, I can set primary_key='slug' and change action_pk_placeholder=':slug' and it will work. But, this logic is misleading since slug is not really the model's primary key